Exploring the principles of spring autowired from an anomaly

Source: Internet
Author: User

Explore the principles of autowired from an anomaly.

First, the environment is this:

 Public class Boss {    @Autowired    private  car car;} // @Component Add this comment, the autowired car of the boss above will fail, the following exception appears  Public class Car {    private  String brand;     Private Double Price ;}

The XML is this:

<?xml version="1.0"encoding="UTF-8"? ><beans xmlns="Http://www.springframework.org/schema/beans"Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="Http://www.springframework.org/schema/context"Xmlns:tx="Http://www.springframework.org/schema/tx"xmlns:aop="HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"xsi:schemalocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp//Www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <context:component-scanBase-package="Com.baobaotao"></context:component-scan> <!--the beanpostprocessor will function automatically and automatically inject the Bean labeled @Autowired--<beanclass="Org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> <!--Remove the attribute injection configuration information for the boss Bean--<bean id="boss" class="Com.baobaotao.Boss"/> <!--<bean id="Office" class="Com.baobaotao.Office"> <property name="Officeno"Value="001"/> </bean> <bean id="Car" class="Com.baobaotao.Car"Scope="Singleton"> <property name="Brand"Value="Red Flag CA72"/> <property name=" Price"Value=" -"/> </bean>--> </beans>

Test class:

Import Com.baobaotao.boss;import Org.junit.test;import org.junit.runner.runwith;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.test.context.contextconfiguration;import Org.springframework.test.context.junit4.abstractjunit4springcontexttests;import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner; the @RunWith (Springjunit4classrunner.class) @ContextConfiguration (Locations= {"Classpath:/beans.xml"}) Public classAutowiredltest extends Abstractjunit4springcontexttests {@Autowired boss boss; @Test Public voidaaa () {System. out. println ("boss ="+boss); }}

As a result, an exception occurs:

D:\soft\java\jdk1.7\bin\java-didea.launcher.port=7533 "-didea.launcher.bin.path=d:\program Files (x86) \jetbrains\intellij idea Community Edition 2016.1.2\bin"-dfile.encoding=utf-8-classpath"D:\soft\java\jdk1.7\jre\lib\charsets.jar;d:\soft\java\jdk1.7\jre\lib\deploy.jar;d:\soft\java\jdk1.7\jre\lib\ Ext\access-bridge-64.jar;d:\soft\java\jdk1.7\jre\lib\ext\dnsns.jar;d:\soft\java\jdk1.7\jre\lib\ext\jaccess.jar ;D: \soft\java\jdk1.7\jre\lib\ext\localedata.jar;d:\soft\java\jdk1.7\jre\lib\ext\sunec.jar;d:\soft\java\jdk1.7\ Jre\lib\ext\sunjce_provider.jar;d:\soft\java\jdk1.7\jre\lib\ext\sunmscapi.jar;d:\soft\java\jdk1.7\jre\lib\ext\ Zipfs.jar;d:\soft\java\jdk1.7\jre\lib\javaws.jar;d:\soft\java\jdk1.7\jre\lib\jce.jar;d:\soft\java\jdk1.7\jre\ Lib\jfr.jar;d:\soft\java\jdk1.7\jre\lib\jfxrt.jar;d:\soft\java\jdk1.7\jre\lib\jsse.jar;d:\soft\java\jdk1.7\jre \lib\management-agent.jar;d:\soft\java\jdk1.7\jre\lib\plugin.jar;d:\soft\java\jdk1.7\jre\lib\resources.jar;d:\ Soft\java\jdk1.7\jre\lib\rt.jar;d:\code\ws\spring\hz\spring-learn\target\test-classes;d:\code\ws\spring\hz\ spring-learn\target\classes; C:\Users\lkms\.m2\repository\junit\junit\4.12\junit-4.12.jar; C:\Users\lkms\.m2\repository\org\hamcRest\hamcrest-core\1.3\hamcrest-core-1.3.jar; C:\Users\lkms\.m2\repository\org\springframework\spring-context\4.3.5.RELEASE\spring-context-4.3.5.RELEASE.jar ; C:\Users\lkms\.m2\repository\org\springframework\spring-aop\4.3.5.RELEASE\spring-aop-4.3.5.RELEASE.jar; C:\Users\lkms\.m2\repository\org\springframework\spring-beans\4.3.5.RELEASE\spring-beans-4.3.5.RELEASE.jar; C:\Users\lkms\.m2\repository\org\springframework\spring-core\4.3.5.RELEASE\spring-core-4.3.5.RELEASE.jar; C:\Users\lkms\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar; C:\Users\lkms\.m2\repository\org\springframework\spring-expression\4.3.5.RELEASE\ Spring-expression-4.3.5.release.jar; C:\Users\lkms\.m2\repository\org\springframework\spring-web\4.3.5.RELEASE\spring-web-4.3.5.RELEASE.jar; C:\users\lkms\.m2\repository\org\springframework\spring-test\4.3.5.release\spring-test-4.3.5.release.jar;d:\ Program Files (x86) \jetbrains\intellij idea Community Edition 2016.1.2\lib\idea_rt.jar"com.intellij.rt.execution.application.AppMain annoioctest November One, . 2: +: theAfternoon Org.springframework.context.support.ClassPathXmlApplicationContext preparerefreshinfo:refreshing Org[email Protected]7a277bd2:startup Date [Sat Nov One  -: +: theCst .]; Root of context hierarchy November One, . 2: +: thepm Org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadbeandefinitionsinfo:loading XML Bean Definitions from classpath resource [beans.xml] November One, . 2: +: *Afternoon Org.springframework.context.support.ClassPathXmlApplicationContext Refreshwarning:exception encountered During context initialization-Cancelling refresh Attempt:org.springframework.beans.factory.UnsatisfiedDependencyException:Error creating bean with Name'boss': Unsatisfied dependency expressed through field'Car'; Nested exception isOrg.springframework.beans.factory.NoSuchBeanDefinitionException:No Qualifying Bean of type'Com.baobaotao.Car'Available:expected at least1Bean which qualifies asAutowire candidate. Dependency annotations: {@org. springframework.beans.factory.annotation.Autowired (required=true)}exceptioninchThread"Main"Org.springframework.beans.factory.UnsatisfiedDependencyException:Error Creating Bean with Name'boss': Unsatisfied dependency expressed through field'Car'; Nested exception isOrg.springframework.beans.factory.NoSuchBeanDefinitionException:No Qualifying Bean of type'Com.baobaotao.Car'Available:expected at least1Bean which qualifies asAutowire candidate. Dependency annotations: {@org. springframework.beans.factory.annotation.Autowired (required=true)} at org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$ Autowiredfieldelement.inject (Autowiredannotationbeanpostprocessor.java:588) at Org.springframework.beans.factory.annotation.InjectionMetadata.inject (Injectionmetadata.java: the) at Org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues ( Autowiredannotationbeanpostprocessor.java:366) at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean ( Abstractautowirecapablebeanfactory.java:1225) at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean ( Abstractautowirecapablebeanfactory.java:552) at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean ( Abstractautowirecapablebeanfactory.java:483) at org.springframework.beans.factory.support.abstractbeanfactory$1. GetObject (Abstractbeanfactory.java:306) at Org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton ( Defaultsingletonbeanregistry.java: the) at Org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean (Abstractbeanfactory.java:302) at Org.springframework.beans.factory.support.AbstractBeanFactory.getBean (Abstractbeanfactory.java:197) at Org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons ( Defaultlistablebeanfactory.java:759) at Org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization ( Abstractapplicationcontext.java:866) at Org.springframework.context.support.AbstractApplicationContext.refresh (Abstractapplicationcontext.java: 542) at Org.springframework.context.support.ClassPathXmlApplicationContext.<init> (Classpathxmlapplicationcontext.java:139) at Org.springframework.context.support.ClassPathXmlApplicationContext.<init> (Classpathxmlapplicationcontext.java: the) at Annoioctest.main (Annoioctest.java:8) at Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) at Sun.reflect.NativeMethodAccessorImpl.invoke (Nativemethodaccessorimpl.java: $) at Sun.reflect.DelegatingMethodAccessorImpl.invoke (Delegatingmethodaccessorimpl.java: +) at Java.lang.reflect.Method.invoke (Method.java:601) at Com.intellij.rt.execution.application.AppMain.main (Appmain.java:144) caused By:org.springframework.beans.factory.NoSuchBeanDefinitionException:No qualifying bean of type'Com.baobaotao.Car'Available:expected at least1Bean which qualifies asAutowire candidate. Dependency annotations: {@org. springframework.beans.factory.annotation.Autowired (required=true)} at Org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound ( Defaultlistablebeanfactory.java:1474) at Org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency ( Defaultlistablebeanfactory.java:1102) at Org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency ( Defaultlistablebeanfactory.java:1064) at org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$ Autowiredfieldelement.inject (Autowiredannotationbeanpostprocessor.java:585)    ...  -Moreprocess finished with exit code1

Spring's start-up process reads the configured XML, registering all beandefinition, which is the preparation process. After the preparation is Beanfactory's refresh, this time will be annotated processing, that is, beanpostprocessor. This includes parsing the various annotations contained in the bean, such as autowired annotations. The postprocesspropertyvalues of Autowiredannotationbeanpostprocessor is inherited from Instantiationawarebeanpostprocessor. is the realization of it. If we look closely at the error stack, perhaps we will find a lot of detail from it.

Reference:

http://blog.csdn.net/mack415858775/article/details/47721909 written very detailed, very good!

Exploring the principles of spring autowired from an anomaly

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.