Skip to content

@Bean(override=true) [SPR-8528] #13172

Closed
Closed

Description

@spring-projects-issues

Stepan Koltsov opened SPR-8528 and commented

Spring javaconf by default allows to override bean definitions. This could cause hard to find bean name collisions made by mistake in large projects.

To avoid this problem Spring provides GenericApplicationContext.setAllowBeanDefinitionOverriding(false). This helps to resolve name collision problems, but it makes impossible (or very hard) reuse of context configuration with minor adjustments (reuse 100 beans, redefine one or two).

I propose @Bean(override=true) parameter.

With @Bean(override=true) new bean is only registered if there already exists a bean with same name. Previous bean definition is removed during this bean registration.

It is an error if @Bean(override=true) overrides nothing.

allowBeanDefinitionOverriding value is ignored when @Bean(override=true)

@Bean(override=true) works for context similarly to how @Override works for classes and interfaces.


Affects: 3.1 M2

Issue Links:

Activity

  1. spring-projects-issues commented on Jul 13, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    Stepan,

    See #12637 as well as the other issues labeled with 'bean-visibility-and-overriding'. This is a theme that we'll be addressing in Spring 3.2.

  2. spring-projects-issues commented on Jul 13, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Stepan Koltsov commented

    Thanks, Chris, waiting for 3.2.

  3. added
    type: enhancement
    in: core
    status: duplicate
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: corestatus: duplicatetype: enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions