Spring Automatic assembly (autowire) error
The error is as follows:
Org.springframework.beans.factory.UnsatisfiedDependencyException:Error creating bean with Name ' person ' defined in Class path resource [Appcontext.xml]: Unsatisfied dependency expressed through Bean property ' axe ':: No Qualifying Bean of type [Com.itheima.app.Axe] is defined:expected single matching beans but found 2:steelaxe, Stoneaxe ; Nested exception is Org.springframework.beans.factory.NoUniqueBeanDefinitionException:No qualifying bean of type [ COM.ITHEIMA.APP.AXE] is defined:expected single matching beans but found 2:steelaxe,stoneaxe
...
caused By:org.springframework.beans.factory.NoUniqueBeanDefinitionException:No qualifying bean of type [ COM.ITHEIMA.APP.AXE] is defined:expected single matching beans but found 2:steelaxe,stoneaxe
At Org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency ( defaultlistablebeanfactory.java:1126)
At Org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency ( DEFAULTLISTABLEBEANFACTORY.JAVA:1014)
At Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType ( abstractautowirecapablebeanfactory.java:1292)
... More
Analysis:
Caused by this line, we are prompted not to qualify the axe type (note: Axe is an interface) bean, found that two implementation beans are matched axe type.
The XML configuration for spring is as follows:
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > <!--<bean id= "person" class= "Com.itheima.app.impl.Chinese" > <property name= "Axe" ref= "Steelaxe" ></ property> <property name= "dog" ref= "Gundog" ></property> </bean> - <BeanID= "Person"class= "Com.itheima.app.impl.Chinese"Autowire= "Bytype"> <!-- here to automatically assemble with the Bytype policy -- </Bean>
<!--Configure the Stellaxe Bean, its implementation class implements the Axe interface--<BeanID= "Steelaxe"class= "Com.itheima.app.impl.SteelAxe" ></Bean><!--Configure the Stoneaxe Bean, and its implementation class also implements the Axe interface--<BeanID= "Stoneaxe"class= "Com.itheima.app.impl.StoneAxe" ></Bean> <BeanID= "Gundog"class= "Com.itheima.app.impl.GunDog" > < Propertyname= "Name"value= "Wangwang"></ Property> </Bean></Beans>
Reason:
Autowire= "Bytype" shows that matching is based on the setter method parameter type and the bean type.
<!--Configure the Stellaxe Bean, its implementation class implements the Axe interface and <bean id= "Steelaxe" class= "Com.itheima.app.impl.SteelAxe" > </bean> <!--configuration Stoneaxe Bean, its implementation class also implements the Axe interface-- <bean id= "Stoneaxe" class= " Com.itheima.app.impl.StoneAxe "></bean>
Look at the annotation explanation, because there are two types of axe in the container that cannot be determined to inject that bean into the person bean, so the program throws an exception.
Solve:
Explicitly specifying dependencies using ref, explicitly specifying dependency overrides for automatic assembly dependencies
<bean id= "Person" class= "Com.itheima.app.impl.Chinese" autowire= "Bytype" >
<property name= "Axe" ref= "Steelaxe" ></property>
</bean>
Spring throws Exception _ Auto Assemble