一、注入分类
bean实例在调用无参构造器创建空值对象后,就要对Bean对象的属性进行初始化。初始化时由容器自动完成的,称为注入。根据注入方式的不同,常用的有2类:设值注入、构造注入。(还有一种,实现特定接口注入,采用侵入式编程,污染了代码,几乎不用)。
二、设值注入
public class Student {
private String name;
private int age;
private School school;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
} public void setSchool(School school) {
this.school = school;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", school=" + school + "]";
} }
Student
public class School {
private String name; public void setName(String name) {
this.name = name;
} @Override
public String toString() {
return "School [name=" + name + "]";
} }
School
public class MyTest { @Test
public void test01() {
//创建容器对象
String resource = "com/jmu/di01/applicationContext.xml";
ApplicationContext ac=new ClassPathXmlApplicationContext(resource);
Student student = (Student) ac.getBean("myStudent");
System.out.println(student);
} }
MyTest
<bean id="mySchool" class="com.jmu.di01.School">
<property name="name" value="清华大学"></property>
</bean>
<bean id="myStudent" class="com.jmu.di01.Student">
<property name="name" value="张三"></property>
<property name="age" value="20"></property>
<property name="school" ref="mySchool"></property>
</bean>
applicationContext.xml
三、构造注入
public class Student {
private String name;
private int age;
private School school; public Student() {
super();
}
public Student(String name, int age, School school) {
super();
this.name = name;
this.age = age;
this.school = school;
} @Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", school=" + school + "]";
} }
Student
public class School {
private String name; public void setName(String name) {
this.name = name;
} @Override
public String toString() {
return "School [name=" + name + "]";
} }
School
<bean id="mySchool" class="com.jmu.di02.School">
<property name="name" value="清华大学"></property>
</bean>
<bean id="myStudent" class="com.jmu.di02.Student">
<constructor-arg name="name" value="李四"/>
<constructor-arg name="age" value="24"/>
<constructor-arg name="school" ref="mySchool"/>
</bean>
applicationContext.xml
四、P命名空间设值注入
五、C命名空间构造注入
六、数组、集合属性注入
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set; public class Some {
private School[] schools;
private String[] myStrs;
private List<String> myList;
private Set<String> mySet;
private Map<String, Object> myMap;
private Properties myPros;//key、value均为字符串
public void setSchools(School[] schools) {
this.schools = schools;
}
public void setMyStrs(String[] myStrs) {
this.myStrs = myStrs;
}
public void setMyList(List<String> myList) {
this.myList = myList;
}
public void setMySet(Set<String> mySet) {
this.mySet = mySet;
}
public void setMyMap(Map<String, Object> myMap) {
this.myMap = myMap;
}
public void setMyPros(Properties myPros) {
this.myPros = myPros;
}
@Override
public String toString() {
return "Some [schools=" + Arrays.toString(schools) + ", myStrs=" + Arrays.toString(myStrs) + ", myList="
+ myList + ", mySet=" + mySet + ", myMap=" + myMap + ", myPros=" + myPros + "]";
} }
Some
public class School {
private String name; public void setName(String name) {
this.name = name;
} @Override
public String toString() {
return "School [name=" + name + "]";
} }
School
<bean id="mySchool" class="com.jmu.di03.School">
<property name="name" value="清华大学"></property>
</bean>
<bean id="mySchool2" class="com.jmu.di03.School">
<property name="name" value="北京大学"></property>
</bean>
<bean id="mySome" class="com.jmu.di03.Some">
<property name="schools">
<array>
<ref bean="mySchool"/>
<ref bean="mySchool2"/> </array>
</property> <property name="myStrs">
<array>
<value>中国</value>
<value>福建</value>
</array>
</property> <property name="myList">
<list>
<value>厦门</value>
<value>泉州</value>
</list>
</property> <property name="mySet">
<set>
<value>唐朝</value>
<value>宋朝</value>
</set>
</property> <property name="myMap">
<map>
<entry key="mobile" value="2132124"></entry>
<entry key="QQ" value="12424532"></entry>
</map>
</property> <property name="myPros">
<props>
<prop key="education">大学</prop>
<prop key="gender">男</prop>
</props>
</property>
</bean>
applicationContext.xml
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { @Test
public void test01() {
//创建容器对象
String resource = "com/jmu/di03/applicationContext.xml";
ApplicationContext ac=new ClassPathXmlApplicationContext(resource);
Some some=(Some) ac.getBean("mySome");
System.out.println(some);
} }
MyTest
输出:
Some [schools=[School [name=清华大学], School [name=北京大学]], myStrs=[中国, 福建], myList=[厦门, 泉州], mySet=[唐朝, 宋朝], myMap={mobile=2132124, QQ=12424532}, myPros={gender=男, education=大学}]
output
简写:
<property name="myStrs" value="中国,福建" />
<property name="myList" value="厦门,泉州" />
<property name="mySet" value="唐朝,宋朝" />
applicationContext.xml
七、对于域属性的自动注入
1、autowire="byName"会从容器中查找与实体类的域属性同名的Bean的id,并将该Bean对象自动注入给该域属性
2、autowire="byType"会从容器中查找与实体类的域属性类型具有is-a关系的Bean,并将该Bean对象自动注入给该域属性
<!-- <bean id="mySchool" class="com.jmu.di06.School">
<property name="name" value="集美大学"></property>
</bean> -->
<bean id="myPrimarySchool" class="com.jmu.di06.PrimarySchool">
<property name="address" value="集美区"></property>
</bean>
<bean id="myStudent" class="com.jmu.di06.Student" autowire="byType">
<property name="name" value="张三"></property>
<property name="age" value="20"></property>
</bean>
applicationContext.xml
public class PrimarySchool extends School {
private String address; public void setAddress(String address) {
this.address = address;
} @Override
public String toString() {
return "PrimarySchool [address=" + address + "]";
} }
PrimarySchool
输出:
Student [name=张三, age=20, school=PrimarySchool [address=集美区]]
output
八、使用SPEL注入
SPEL,Spring Expression Language,即Spring EL表达式语言。在Spring配置文件中为Bean的属性注入时,可直接使用SPEL表达式计算的结果。
<bean id="myPerson" class="com.jmu.di07.Person">
<property name="pname" value="宫本武藏"></property>
<property name="page" value="#{T(java.lang.Math).random()*50}"></property>
</bean>
<bean id="myStudent" class="com.jmu.di07.Student" autowire="byType">
<property name="name" value="#{myPerson.pname}"></property>
<property name="age" value="#{myPerson.page>25?25:myPerson.page}"></property>
</bean>
applicationContext.xml
Person person=(Person) ac.getBean("myPerson");
System.out.println(person); Student student = (Student) ac.getBean("myStudent");
System.out.println(student);
MyTest
输出:
Person [pname=宫本武藏, page=15]
Student [name=宫本武藏, age=15]
output
九、内部Bean
十、同类抽象Bean
十一、异步抽象Bean
十二、为应用指定多个Spring配置文件
1、平等关系
方式一:
方式二:
二、包含关系