Spring (iv) Bean injection test

Source: Internet
Author: User

First, the construction method injects

Definition: Completion of dependency setting by constructor function

Advantages and Disadvantages

When constructing the object, complete the establishment of the dependency relationship

If you have a lot of objects associated with it, and you have to add too many arguments to the constructor method

The base has index: If the specified index starts at 0, the type is used to specify types

Entity class:

 Package  com.pb.entity;/** * Class class * @author Administrator * */public class  Grade {private int id; Class number private String name;        Class name Public  Grade () {Super  ();  TODO auto-generated constructor stub } public Grade (int  ID, String name) {Super  (); this.id =  id; this.name =  name,} public int  getId () {return  ID,} public void setId (int  id) {thi S.id =  ID;} public  String GetName () {return  name,} public void  setName (String name) {this.name =  name;}               
Package com.pb.entity;/** * Student class * @author Administrator * */public class Student {    private String name;    Student name    private Integer age;   Student Age    Private Grade Grade;  Class public         Student () {        Super();    }            Public Student (String name, Integer-age, Grade-Grade) {        Super(); this.name = name; this.age = Age ; This.grade = grade,} public String GetName () {return name,} public void setName (String name) {This . Name = name,} public Integer getage () {return age ,} public void setage (Integer age) {This.age =
                   
                     age; } public
                     Grade Getgrade () {return Grade,} public void Setgrade (Grade Grade) {this.grade = Grade;} }
                    

Using construction methods to inject

Applicationcontext.xml

 <?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:p= "http://www.springframework.org/schema/p" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.1.xsd "><!--class--><bean id=" Grade "class=" Com.pb.entity.Grade "><!--using construction method injection-- ><constructor-arg><value>1001</value></constructor-arg><constructor-arg>< Value> Computing Application class </value></constructor-arg></bean><!--Student class--><bean id= "student" class= " Com.pb.entity.Student "><!--injected--><constructor-arg><value> Zhang San using a construction method </value></ constructor-arg><constructor-arg><value>23</value></constructor-arg><!-- Use ref to inject class bean--><constructor-arg ref= "grade" ></CONSTRUCTOR-ARG></BEAN></BEANS> 

Test class:

 PackageCom.pb.demo;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;Importcom.pb.entity.Student; Public classDemo1 { Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Student Stu= Context.getbean ("Student", student.class); System.out.println ("Student Name:" + stu.getname () + "Student Age:" +stu.getage ()+ "Student class Number:" + stu.getgrade (). GetId () + "Student class Name:" +Stu.getgrade (). GetName ()); }}

Second, attribute injection

Other unchanged only change configuration file

Applicationcontext.xml

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"><!--Class Classes -<BeanID= "Grade"class= "Com.pb.entity.Grade">< Propertyname= "id"><value>1001</value></ Property>< Propertyname= "Name"value= "Computer Application one class"></ Property></Bean><!--Student Class -<BeanID= "Student"class= "Com.pb.entity.Student">< Propertyname= "Name"value= "Zhang San" />< Propertyname= "Age"value= "+"/>< Propertyname= "Grade"ref= "Grade"/></Bean></Beans>

Third, inject null and empty string values

<value></value> represents an empty string

<null></null> represents a null value

class= "Com.pb.entity.Student" ><!--name injects an empty string <value></value> represents an empty string--><property Name= "Name" ><value></value></property><!--age injected as a null value  --><property name= "ages" ><null></null></property><property name= "Grade" ref= "Grade"/></ Bean>

Iv. injecting beans with the P-namespace

The official recommended injection method

Needs to be added to the XML

xmlns:p= "http://www.springframework.org/schema/p"
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">    <!--class class using the P namespace injection -    <BeanID= "Grade"class= "Com.pb.entity.Grade"P:id= "1001"P:name= "Foreign Language class">    </Bean>    <!--Student class using the P namespace injection -    <BeanID= "Student"class= "Com.pb.entity.Student"P:name= "Zhang San"P:age= "All"P:grade-ref= "Grade">    </Bean></Beans>

Results at a glance

Five, automatic assembly

You need to use the Autowire property to configure

You can use Autowire in each bean to configure

You can also use the Autowire global configuration in <beans> to indicate that this beans uses automatic assembly,

Disadvantages: Not clear, there are problems more difficult to find

Autowire:

No (default): The specified dependent object must be displayed without automatic assembly

ByName: Automatically assembled according to the attribute name. Automatically finds the same ID as the property name, and if found, injects it automatically, otherwise nothing is done

Bytype: Automatic assembly based on the type of the property, Spring automatically finds the bean with the same type of attribute, if it just finds the only one, it is automatically injected, if more than one bean with the same property type is found, throws an exception and does nothing if it is not found.

Constructor: Similar to Bytype, but it is for construction methods, if a bean is found to match the parameter type of the constructor method, the dependent object is injected through the construct and throws an exception if it is not found.

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">    <!--class class using the P namespace injection -    <BeanID= "Grade"class= "Com.pb.entity.Grade"P:id= "1001"P:name= "Foreign Language class" >    </Bean>    <!--Student class using the P namespace injection -    <BeanID= "Student"class= "Com.pb.entity.Student"P:name= "Zhang San"P:age= "All"Autowire= "ByName">    </Bean></Beans>

Automatic assembly makes the configuration file very concise, but it also causes the dependencies between the components to be ambiguous, prone to some potential errors, and use with caution

Spring (iv) Bean injection test

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.