我们有两个实体类Student和Teacher,Studnet对象需要依赖一个Teacher对象,可以在applicationContext.xml文件中进行配置。
public class Student {
private int id;
private String name;
private int age;
private int tid;
private Teacher teacher; //需要一个Teacher对象
public Student() {
}
public Student(int id, String name, int age, int tid) {
this.id = id;
this.name = name;
this.age = age;
this.tid = tid;
}
public Teacher(int id, String name) {
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;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getTid() {
return tid;
}
public void setTid(int tid) {
this.tid = tid;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
public class Teacher {
private int id;
private String name;
public Teacher() {
}
public Teacher(int id, String name) {
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;
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student5" class="com.bear.sxt.pojo.Student">
<property name="teacher" ref="teacher"/>
bean>
<bean id="teacher" class="com.bear.sxt.pojo.Teacher">
<property name="id" value="1"/>
<property name="name" value="DI测试"/>
bean>
beans>
观察上面的栗子我们可以看到Student对象中所引用的Teacher对象的属性名与Teacher对象的id是一样的。Spring提供了自动注入的方式,可以自动匹配注入对象与被注入对象。即无需进行下面的配置也可以完成依赖注入。
<property name="teacher" ref="teacher"/>
自动注入方式
配置applicationContext.xml文件,bean标签中的autowire属性进行自动注入。autowire标签有5个可选值。
public Student(Teacher teacher) {
this.teacher = teacher;
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byName">
<bean id="teacher" class="com.bear.sxt.pojo.Teacher">
<property name="id" value="1"/>
<property name="name" value="张三"/>
bean>
<bean id="student1" class="com.bear.sxt.pojo.Student">
<property name="teacher" ref="teacher"/>
bean>
<bean id="student2" class="com.bear.sxt.pojo.Student" autowire="byName"/>
<bean id="student3" class="com.bear.sxt.pojo.Student" autowire="byType"/>
<bean id="student4" class="com.bear.sxt.pojo.Student" autowire="constructor"/>
<bean id="student5" class="com.bear.sxt.pojo.Student" autowire="default"/>
<bean id="student6" class="com.bear.sxt.pojo.Student" autowire="no"/>
beans>
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student1 = ac.getBean("student1", Student.class);
Student student2 = ac.getBean("student2", Student.class);
Student student3 = ac.getBean("student3", Student.class);
Student student4 = ac.getBean("student4", Student.class);
Student student5 = ac.getBean("student5", Student.class);
Student student6 = ac.getBean("student6", Student.class);
System.out.println("1" + student1.getTeacher());
System.out.println("2" + student2.getTeacher());
System.out.println("3" + student3.getTeacher());
System.out.println("4" + student4.getTeacher());
System.out.println("5" + student5.getTeacher());
System.out.println("6" + student6.getTeacher());
}
}