Understanding Spring-2.3 Annotation assembly from the Beginning-@autowired (4)-required (1)

Source: Internet
Author: User

In this chapter we will discuss in detail the parameter required inside the @autowired.

1.domain (Focus)

Cake Type:

Package Com.raylee.my_new_spring.my_new_spring.ch02.topic_1_9;public class Cake {private String name = "";p ublic string GetName () {return name;} public void SetName (String name) {this.name = name;}}

Chef class:

(1) The following is an error in the Chef class

Package Com.raylee.my_new_spring.my_new_spring.ch02.topic_1_9;import Org.springframework.beans.factory.annotation.autowired;public class Chief {@Autowiredprivate Cake Cake = null;private String name = "";p ublic String getName () {return name;} Public Cake Makeonecake () {if (Cake! = null) {System.out.println (GetName () + "make" + cake.getname ());} else {System.out.println (cake);} return cake;} public void SetName (String name) {this.name = name;}}

Error message:

caused by:org.springframework.beans.factory.NoSuchBeanDefinitionException:No matching bean of type [com.raylee.my_ New_spring.my_new_spring.ch02.topic_1_9.Cake] found for dependency:expected at least 1 bean which qualifies as Autowire C Andidate for this dependency. Dependency annotations: {@org. springframework.beans.factory.annotation.Autowired (Required=true)}
At Org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException ( defaultlistablebeanfactory.java:920)
At Org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency ( defaultlistablebeanfactory.java:789)
At Org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency ( defaultlistablebeanfactory.java:703)
At org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$ Autowiredfieldelement.inject (autowiredannotationbeanpostprocessor.java:474)
... More


(2) The following is an empty object allowed to inject

Package Com.raylee.my_new_spring.my_new_spring.ch02.topic_1_9;import Org.springframework.beans.factory.annotation.autowired;public class Chief {@Autowired (required = false) Private Cake Cake = null;private String name = "";p ublic string GetName () {return name;} Public Cake Makeonecake () {if (Cake! = null) {System.out.println (GetName () + "make" + cake.getname ());} else {System.out.println (cake);} return cake;} public void SetName (String name) {this.name = name;}}

It is important to note that the @Autowired tag is the default force injection of non-empty objects, but we can inject a null object through the Required=false property


2. Test class: (unchanged)

Package Com.raylee.my_new_spring.my_new_spring.ch02.topic_1_9;import Org.junit.test;import Org.junit.runner.runwith;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.context.applicationcontext;import org.springframework.test.context.ContextConfiguration; Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith (Springjunit4classrunner.class) @ Contextconfiguration (locations = {"/com/raylee/my_new_spring/my_new_spring/ch02/topic_1_9/ Applicationcontext-test.xml "}) public class Chieftest {@Autowiredprivate ApplicationContext applicationcontext;@ testpublic void Testchief () {Chief Jack = Applicationcontext.getbean (Chief.class); Jack.makeonecake ();}}


3. configuration file (emphasis)

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: context= "Http://www.springframework.org/schema/context" xmlns:p= "http://www.springframework.org/schema/p" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:aop= " Http://www.springframework.org/schema/aop "xmlns:util=" Http://www.springframework.org/schema/util "xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.1.xsd Http://www.springframework.org/schema/util http://www.springframework.org/schema/util/ Spring-util-2.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-3.1.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/ Spring-util-3.1.xsdhttP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA http://www.springframework.org/schema/data/jpa/ Spring-jpa-1.0.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop.xsd "><bean id=" Jack "class=" Com.raylee.my_new_spring.my_new_spring.ch02.topic_1_9.Chief "p:name= "Jack"/></beans>

To test the example above, we deliberately deleted the cake bean.


Summary: In this chapter we mainly discuss the parameter required inside the @autowired.


Catalog: http://blog.csdn.net/raylee2007/article/details/50611627

My github:https://github.com/raylee2015/my_new_spring.



Understanding Spring-2.3 Annotation assembly from the beginning [email protected](4)-required (1)

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.