依赖注入(ID)是Spring协调不同Bean实例之间的合作而提供的一种工作机制,在确保Bean实例之间合作的同时,并能保持每个Bean的相对独立性。在Spring框架下,当Bean实例 A运行过程中需要引用另外一个Bean实例B时,Spring框架会创建Bean的实例B,并将实例B通过实例A的构造函数、set方法、自动装配和注解方式注入到实例A,这种注入实例Bean到另外一个实例Bean的过程称为依赖注入(DI)。
- 基于构造函数的依赖注入
- 基于设置函数的依赖注入(Setter方法)
- 工厂方式的依赖注入
- 基于自动装配的依赖注入
- 基于注解的依赖注入
Setter 注入
基本类型值注入(value)
(1)Student类
public class Student {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
(2)bean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="com.haan.di.Student">
<property name="name" value="libai"/>
<property name="age" value="22"/>
</bean>
</beans>
(3)测试
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
Student student = context.getBean(Student.class);
System.out.println( student.toString());
}
}
//Student{name='libai', age=22}
引用类型值注入(ref)
(1)class
AddressInfo.class
public class AddressInfo {
private String tel; //基本类型
private String city;
public void setTel(String tel) {
this.tel = tel;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "AddressInfo{" +
"tel='" + tel + '\'' +
", city='" + city + '\'' +
'}';
}
}
Student2.class
public class Student2 {
private String name; //基本类型
private int age; //基本类型
private AddressInfo addressInfo; //引用类型
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setAddressInfo(AddressInfo addressInfo) {
this.addressInfo = addressInfo;
}
@Override
public String toString() {
return "Student2{" +
"name='" + name + '\'' +
", age=" + age +
", addressInfo=" + addressInfo +
'}';
}
}
(2)bean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student2" name="student2" class="com.haan.di.Student2">
<property name="name" value="libai" />
<property name="age" value="22"/>
<property name="addressInfo" ref="addressInfo"/>
</bean>
<bean id="addressInfo" class="com.haan.di.AddressInfo">
<property name="tel" value="1008611"/>
<property name="city" value="上海" />
</bean>
</beans>
(3)main函数测试
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
Student2 student2 = context.getBean(Student2.class);
System.out.println( student2.toString());
}
}
使用 p-namespace 实现 XML 配置
构造函数注入
当容器调用带有一组参数的类构造函数时,基于构造函数的 DI 就完成了,其中每个参数代表一个对其他类的依赖。
单个有参构造方法注入
<bean id="person1" class="com.haan.di.Person">
<constructor-arg name="name" value="libai"/>
</bean>
type属性
Index属性
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
Person person1 = (Person) context.getBean("person1");
System.out.println("person1"+person1.toString());
Person person2 = (Person) context.getBean("person2");
System.out.println("person2"+person2.toString());
Person person3 = (Person) context.getBean("person3");
System.out.println("person3"+person3.toString());
}
}
<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="person1" class="com.haan.di.Person">
<constructor-arg name="name" value="libai"/>
</bean>
<bean id="person2" class="com.haan.di.Person">
<constructor-arg type="java.lang.String" value="zhangfei"/>
<constructor-arg type="java.lang.Integer" value="22"/>
</bean>
<bean id="person3" class="com.haan.di.Person">
<constructor-arg index="0" value="zhangfei"/>
<constructor-arg index="1" value="22"/>
<constructor-arg index="2" value="1008611"/>
</bean>
<bean id="test1" class="com.haan.test.Test1" />
<bean id="test2" class="com.haan.test.Test2" />
</beans>
public class Person {
private String name;
private Integer age;
private String tel;
public Person(){}
public Person(String name){
this.name = name;
}
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
public Person(String name, Integer age, String tel) {
this.name = name;
this.age = age;
this.tel = tel;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", tel='" + tel + '\'' +
'}';
}
}
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
Person person1 = (Person) context.getBean("person1");
System.out.println("person1"+person1.toString());
Person person2 = (Person) context.getBean("person2");
System.out.println("person2"+person2.toString());
Person person3 = (Person) context.getBean("person3");
System.out.println("person3"+person3.toString());
}
//person1Person{name='libai', age=null, tel='null'}
//person2Person{name='zhangfei', age=22, tel='null'}
//person3Person{name='zhangfei', age=22, tel='1008611'}
}