Spring throws Exception _ Auto Assemble

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.