Spring Notes (DI (assigning values to attributes), automatic Assembly (Autowire))

Source: Internet
Author: User

four ways to assign a value to a property 1.DI:01.Student entity class:
 PackageCn.pb.bean;/*** Student entity class*/ Public classStudent {PrivateString name;//name    PrivateInteger age;//Age    PrivateGrade Grade;//Grade@Override PublicString toString () {return"Student [name=" + name + ", age=" + Age + ", grade=" +Grade+ "]"; }    //P must have a non-parametric structure when injected     PublicStudent () {Super(); }    //C must have a parametric structure when injected.     PublicStudent (String name, Integer age, Grade Grade) {Super();  This. Name =name;  This. Age =Age ;  This. grade =grade; }    //P must have set () when injected.     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicInteger getage () {returnAge ; }     Public voidsetage (Integer age) { This. Age =Age ; }     PublicGrade Getgrade () {returngrade; }     Public voidSetgrade (Grade Grade) { This. grade =grade; }}
02.Grade entity class:
 PackageCn.pb.bean;/*** Grade entity class*/ Public classGrade {PrivateString name;//Grade name@Override PublicString toString () {return"Grade [name=" + name + "]"; }     PublicGrade () {Super(); }     PublicGrade (String name) {Super();  This. Name =name; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }}

03.DI Injection Method:
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans"xmlns:p= "http://www.springframework.org/schema/p"Xmlns:c= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/C"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation="http//Www.springframework.org/schema/beanshttp//www.springframework.org/schema/beans/spring-beans.xsd ">   <!--bean--> of the grade corresponding to the configuration<bean id= "Grade"class= "Com.xdf.bean.Grade" ><!--01. Set Value injection (recommended, easy to read) there must be a set method in the corresponding class, because the underlying execution reflection mechanism queries the corresponding setxxx (DI) in the class .<property name= "Gradeid" value= "1"/> <property name= "name" value= "first grade"/> </bean><!--Configuring a student's corresponding bean 02.P namespace assignment must have set () and no parameter constructs<bean id= "Student"class= "Com.xdf.bean.Student"P:age= "P:name=" "Black" p:grade-ref= "Grade"/>--><!--03. Assigning Values to a property by constructing a method requires that the corresponding parameter construction method does not require set and get, and no parameter construction is required<bean id= "Student"class= "Com.xdf.bean.Student" >001: Use subscript for parameter<constructor-arg index= "0" value= "Xiaohei"/> <constructor-arg index= "1" value= "/> <constru" Ctor-arg index= "2" ref= "Grade"/>002: Use the name of the parameter<constructor-arg name= "name" value= "Xiaohei"/> <constructor-arg name= "age" value= "/> <cons" Tructor-arg name= "Grade" ref= "Grade"/>003: Default order for Using Parameters<constructor-arg value= "Xiaohei"/> <constructor-arg value= "/> <constructor-arg ref=" gra De "/> </bean>--><!--04. Assigning a property to an attribute by C namespace (constructor method) is necessary to have a corresponding parameter construction method--<bean id= "Student"class= "Com.xdf.bean.Student"C:age= "C:name=" "Xiaobai" c:grade-ref= "Grade"/></beans>
04. Test the Code:
 PackageCN.PB;Importcn.pb.bean.Student;Importorg.junit.Test;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classstudenttest {@Test Public  voidtest01 () {ApplicationContext context=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); /** Get container give us the student object created by the IOC's embodiment * itself created by itself the process of object creation, the right to create objects handed over to the spring container!         IOC Control Inversion * * before Student student=new Student ();         * Now Context.getbean ("student"); * Context container to create objects **/Student Student= (Student) context.getbean ("Student");    SYSTEM.OUT.PRINTLN (student); }}

2. Automatic assembly (Autowire): 01. Master Entity class:
/*** Lord Human*/ Public classPerson {PrivateString name;//name     Private  intAge//Age     PrivateDog Dog;//Owner's Pet     PrivateCat Cat;//Owner's Pet     PublicCat Getcat () {returnCat; }     Public voidSetcat (cat cat) { This. cat =Cat; }     PublicDog Getdog () {returnDog; }     Public voidSetdog (dog dog) { This. Dog =Dog; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; } @Override PublicString toString () {return"person{" + "name=" + name + ' \ ' + ", age=" + Age + ", dog=" + Dog + ", cat=" + Cat + '} '; }     PublicPerson (String name,intAge ) {         This. Name =name;  This. Age =Age ; }     PublicPerson () {}}
02. Pet Dog Entity class:
/*** Pet Dog class*/ Public classDog {PrivateString name;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; } @Override PublicString toString () {return"dog{" + "name=" + name + ' \ ' + '} '; }}
03. Pet Dog Sub-class puppy entity class:
/**
* Inherit the pet dog so the pet dog's non-private thing he has all
*/
publicclass Smalldog extends Dog {}

04. Pet Cat Entity class:
/*** Kitten Type*/ Public classCat {PrivateString name;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; } @Override PublicString toString () {return"dog{" + "name=" + name + ' \ ' + '} '; }}

05.applicationcontext.xml configuration file (automatic assembly):
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation="http//Www.springframework.org/schema/beanshttp//www.springframework.org/schema/beans/spring-beans.xsd "><!--autowire Automatic assembly of objects in two ways 01.byName spring will find the name of the attribute in the XML file to assemble the bean with the ID property name in it. 02.byType Spring will find an XML file based on the type of attribute in the entity class to find a class that is an attribute type to assemble, if there are multiple errors! -< owner of the!--configuration bean--> <bean id= "Person"class= "Cn.pb.bean.Person" autowire= "byname" > <property name= "Age" value= "" "/> <property name=" name " Value= "Xiaohei"/><!--Direct Reference Autowire property has no effect <property name= "dog" ref= "dog"/>--></bean><!--configuration of pet dog bean--><bean id= "Dog"class= "Cn.pb.bean.Dog" > <property name= "name" value= "Husky"/> </bean><!--puppy bean--><bean id= "Smalldog"class= "Cn.pb.bean.SmallDog" ><!--Smalldog does not have any properties, but inherits the name--> of the parent class<property name= "name" value= "Puppy Husky"/> </bean><!--owner's second pet kitty---<bean id= "Cat"class= "Cn.pb.bean.Cat" > <property name= "name" value= "machine anchor"/> </bean></beans>
06. Test the code:
 Public class Persondemo {    publicstaticvoid  main (string[] args) {        ApplicationContext Context=new  classpathxmlapplicationcontext                (" Applicationcontext.xml ");       // Get host information        Person person = (person) context.getbean ("person");        SYSTEM.OUT.PRINTLN (person);    }}



Spring Notes (DI (assigning values to attributes), automatic Assembly (Autowire))

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.