Filter out non-autowire candidates for calls to getBean(Class<?>) [SPR-7120] #11780
Labels
in: core
Issues in core modules (aop, beans, core, context, expression)
type: enhancement
A general enhancement
Milestone
Chris Beams opened SPR-7120 and commented
Consider a scoped proxy bean of type
Foo
with bean namefoo
. If a user attempts to fetch this bean by type from the container usinggetBean(Foo.class)
, aNoSuchBeanDefinitionException
will be thrown stating that there is an ambiguity because two beans of typeFoo
exist:[foo, scopedTarget.foo]
. As it is almost certainly the user's intent to retrieve the proxy and not the target, we should filter out the scoped target before evaluating whether there is an ambiguity.A nice way of implementing this is to simply check whether each bean definition of the matching type is an autowire candidate. If not, filter it from the list. In this way, the list in the example above will be narrowed to exactly one bean, and that bean will be returned from the call to
getBean(Foo.class)
.Attachments:
Referenced from: commits 3f06a92
The text was updated successfully, but these errors were encountered: