Autowiring Spring superclass

怎甘沉沦 提交于 2019-12-07 03:07:52

问题


Why does Spring automatically choose the superclass types during autowiring?

For instance, if I have

@Component
public class Foo {}

@Component
public class Bar extends Foo {}

and someone autowires

@Autowired
private Foo foo;

How come Spring always chooses the supertype Foo? Shouldn't this be an "ambiguous" mapping (and cause Spring to throw an error)?

Don't you technically have two Foo candidates? (e.g., Bar gets automatically picked when @Component is removed from Foo...)


回答1:


That might be because the autowiring is done by name, not type. If I setup my bean using xml like this:

<bean id="foo1" class="Foo"/>
<bean id="foo2" class="Bar"/>

And attempt to autowire by type:

@Autowired private Foo aFoo;

I get

org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [Foo]



回答2:


Autowiring can work using both type and bean name, depending on how you have it configured.

In this case, since there are two beans of type Foo, the Foo instance may be chosen because it matches the name of the variable foo.

What happens if you rename foo to something else?




回答3:


If there are two beans of same type , then spring tries to resolve dependence by the name of the variable you have specified. If the name does not match with any of the bean names, then it will throw an error. But, if it finds a bean name matching to that of variable name you specified, it will inject that bean. So, while injecting dependencies, spring considers both , the type and the name.



来源:https://stackoverflow.com/questions/16683408/autowiring-spring-superclass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!