Spring (4) Bean injection (Spring Bean injection)
1. constructor Injection
Definition: You can use constructors to set dependencies.
Advantages and disadvantages:
Complete dependency creation while constructing objects
If there are many associated objects, you have to add too many parameters to the constructor.
Base index: if the specified index starts from 0, type is used to specify the type.
Entity class:
Package com. pb. entity;/*** 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) {this. 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; // 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 ;}}
Use constructor Injection
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"> <! -- Use constructor to inject data --> <constructor-arg> <value> 1001 </value> </constructor-arg> <value> Computing Application Class 1 </ value> </constructor-arg> </bean> <! -- Student --> <bean id = "Student" class = "com. pb. entity. student"> <! -- Inject using constructor-arg> <value> Zhang San </value> </constructor-arg> <value> 23 </value> </constructor-arg> <! -- Inject class bean with ref --> <constructor-arg ref = "grade"> </constructor-arg> </bean> </beans>
Test class:
Package com. pb. demo; import org. springframework. context. applicationContext; import org. springframework. context. support. classPathXmlApplicationContext; import com. pb. entity. student; public class Demo1 {public static void main (String [] args) {ApplicationContext context = new ClassPathXmlApplicationContext ("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 ());}}
Ii. Property Injection
Other configuration files unchanged
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 "> <property name =" id "> <value> 1001 </value> </property> <property name =" name "value =" Computer Application Class 1 "> </property> </bean> <! -- Student --> <bean id = "student" class = "com. pb. entity. student "> <property name =" name "value =" Zhang San "/> <property name =" age "value =" 18 "/> <property name =" grade "ref = "grade"/> </bean> </beans>
3. Inject NULL and NULL string values
<Value> </value> indicates an empty string.
<Null> </null> indicates a null value.
<Bean id = "student" class = "com. pb. entity. Student"> <! -- Name Injection Null String <value> </value> indicates Null String --> <property name = "name"> <value> </property> <! -- Age injection is NULL --> <property name = "age"> <null> </property> <property name = "grade" ref = "grade "/> </bean>
4. Inject bean using the p namespace
Officially recommended injection methods
You need to add
xmlns:p="http://www.springframework.org/schema/p"
<? 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 uses p namespace injection --> <bean id = "grade" class = "com. pb. entity. grade "p: id =" 1001 "p: name =" "> </bean> <! -- Use p namespace injection for student class --> <bean id = "student" class = "com. pb. entity. student "p: name =" James "p: age =" 23 "p: grade-ref =" grade "> </bean> </beans>
Clear results
V. Automatic Assembly
You need to use the autowire attribute to configure
You can use autowire to configure Each bean.
You can also use the autowire global configuration in <beans> to indicate that all of the beans use automatic assembly,
Disadvantages: unclear, difficult to find problems
Autowire:
No (default value): The specified dependent object must be displayed if automatic assembly is not performed.
ByName: Automatic Assembly Based on the property name. Automatically finds the id with the same attribute name. If the id is found, it is automatically injected; otherwise, nothing will be done.
ByType: Automatic Assembly Based on the property type. Spring automatically searches for beans with the same property type. If only one Bean is found, automatic injection is performed, if multiple beans with the same property type are found, an exception is thrown. If no Bean is found, nothing is done.
Constructor: similar to byType. However, for constructor, if a Bean matches the parameter type of the constructor, the dependent object is injected through constructor. If the constructor cannot be found, an exception is thrown.
<? 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 uses p namespace injection --> <bean id = "grade" class = "com. pb. entity. grade "p: id =" 1001 "p: name =" "> </bean> <! -- Use p namespace injection for student class --> <bean id = "student" class = "com. pb. entity. student "p: name =" James "p: age =" 23 "autowire =" byName "> </bean> </beans>
Automatic Assembly makes the configuration file very concise, but it also causes unclear dependencies between components, which may easily lead to some potential errors. Use it with caution.