Description
Emerson Farrugia opened SPR-10808 and commented
I've hit an issue where a single component scan was finding two @Beans
with the same name. The beans weren't intentionally named the same, it was just an oversight. The container initialised one bean and ignored the other, logging at info instead of warn. The logging level is the first problem, since logging at info makes it much more likely the message is missed. The second problem is that the ignored bean was explicitly marked as @Primary
. I've attached files with an example.
As far as I can tell, there's no way to control which bean gets picked in this situation. @Order
on the @Configuration
doesn't help.
In terms of fixes, I suggest the info message gets changed to warning. I'd personally even go one further. Given the functionality of @Profile
, @Primary
, and that autowiring by type is encouraged, does bean overriding still make sense in 3.1+.x? @Profile
and @Primary
give us a deterministic way of controlling autowire candidates. In contrast, quiet non-deterministic overrides by name seem too fragile to fit.
Affects: 3.2.3
Attachments:
- Bar.java (101 bytes)
- BarConfiguration.java (446 bytes)
- Foo.java (101 bytes)
- FooConfiguration.java (378 bytes)
- Launcher.java (712 bytes)
- SomeInterface.java (90 bytes)
Issue Links:
- Allow @Bean methods to override definitions in XML [SPR-7028] #11690 Allow
@Bean
methods to override definitions in XML - When a bean definition is overridden, detect whether the definition has changed and log accordingly [SPR-7607] #12263 When a bean definition is overridden, detect whether the definition has changed and log accordingly
- Allow @Bean to override a dynamically registered bean definition [SPR-10607] #15236 Allow
@Bean
to override a dynamically registered bean definition - @Primary annotation is not working for beans with the same name [SPR-13980] #18552
@Primary
annotation is not working for beans with the same name - Regression: @Primary annotation not working when overriding bean definition with classpath scanning [SPR-12934] #17527 Regression:
@Primary
annotation not working when overriding bean definition with classpath scanning - Auto-resolve bean name conflicts for scanned classes [SPR-14665] #19229 Auto-resolve bean name conflicts for scanned classes
2 votes, 7 watchers