Skip to content

Allow @Bean methods to override definitions in XML [SPR-7028] #11690

Closed
@spring-projects-issues

Description

@spring-projects-issues

Dave Syer opened SPR-7028 and commented

There are two ways I might want to do this, and both fail.

  1. Use @ImportResource in an @Configuration to load an XML file, then override a bean using @Bean
  2. Write an XML file that imports another file and then defines a bean that is @Configuration, and the latter provides a bean with the same name

Neither works.

Case 1 fails because ConfigurationClassBeanDefinitionReader always loads XML imports after the @Bean definitions. This seems like the wrong order, so it would be good to understand why it is implemented that way (I imported before I defined the @Bean, so I expect the latter to win).

This code in ConfigurationClassBeanDefinitionReader prevents the override in case 2:

if (!(existingBeanDef instanceof ConfigurationClassBeanDefinition)) {
	// no -> then it's an external override, probably XML
	// overriding is legal, return immediately
	return;
}

Affects: 3.0.1

Issue Links:

8 votes, 12 watchers

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: declinedA suggestion or change that we don't feel we should currently applytype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions