David Lopez opened SPR-15805 and commented

When you define a bean in a XML, you cannot redefine it in a @Component or @TestComponent class using a @Bean declaration). It can, however, be redefined using another XML.

I have defined this as a Improvement since I do not think it explicitly violates anything .. simply it is a grey zone in the DI part fo Spring, but anyway, I consider this behaviour inconsistent and weird.

This issue is similar to #14201, but to the case when


Affects: 4.3.9, 5.0 RC2

Attachments: - XmlOverrideBug.zip (5.35 kB)

Issue Links: - #14201 Beans defined by @ComponentScan are skipped in @Configuration override

1 votes, 1 watchers

Comment From: spring-projects-issues

David Lopez commented

Added an example: it is a dummy project that defines two possible implementations of a simple interface.

There is three ways to define a bean named "stupidObject": by annotation in (the class A), in a @Configuration class (which created an A object) and importing an XML (which defined it as a instance of A too).

There is an unit test that tries to redefine the bean "stupidObject" using a @TestConfiguration inner class by the second implementation B. It successes in the case of defining the bean using annotations or Java Config. But when the instance is defined thru XML, it cannot be redefined.

The test used Spring Boot, but it is not really necessary to check this scenario.

Comment From: snicoll

Thanks for the sample, I did upgrade it to a recent version and can reproduce the behavior. XML bean definitions can't be overridden by java config bean in the sense that these are "top-level" and @Bean methods aren't (only the @Configuration class is).

In general, we feel that bean overriding shouldn't be used at all. This triggers the discussion of deprecating the feature altogether, see #31288