依赖注入(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类

  1. public class Student {
  2. private String name;
  3. private int age;
  4. public void setName(String name) {
  5. this.name = name;
  6. }
  7. public void setAge(int age) {
  8. this.age = age;
  9. }
  10. @Override
  11. public String toString() {
  12. return "Student{" +
  13. "name='" + name + '\'' +
  14. ", age=" + age +
  15. '}';
  16. }
  17. }

(2)bean.xml 配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <bean id="student" class="com.haan.di.Student">
  6. <property name="name" value="libai"/>
  7. <property name="age" value="22"/>
  8. </bean>
  9. </beans>

(3)测试

  1. public class Main {
  2. public static void main(String[] args) {
  3. ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
  4. Student student = context.getBean(Student.class);
  5. System.out.println( student.toString());
  6. }
  7. }
  8. //Student{name='libai', age=22}

引用类型值注入(ref)

(1)class
AddressInfo.class

  1. public class AddressInfo {
  2. private String tel; //基本类型
  3. private String city;
  4. public void setTel(String tel) {
  5. this.tel = tel;
  6. }
  7. public void setCity(String city) {
  8. this.city = city;
  9. }
  10. @Override
  11. public String toString() {
  12. return "AddressInfo{" +
  13. "tel='" + tel + '\'' +
  14. ", city='" + city + '\'' +
  15. '}';
  16. }
  17. }

Student2.class

  1. public class Student2 {
  2. private String name; //基本类型
  3. private int age; //基本类型
  4. private AddressInfo addressInfo; //引用类型
  5. public void setName(String name) {
  6. this.name = name;
  7. }
  8. public void setAge(int age) {
  9. this.age = age;
  10. }
  11. public void setAddressInfo(AddressInfo addressInfo) {
  12. this.addressInfo = addressInfo;
  13. }
  14. @Override
  15. public String toString() {
  16. return "Student2{" +
  17. "name='" + name + '\'' +
  18. ", age=" + age +
  19. ", addressInfo=" + addressInfo +
  20. '}';
  21. }
  22. }

(2)bean.xml配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <bean id="student2" name="student2" class="com.haan.di.Student2">
  6. <property name="name" value="libai" />
  7. <property name="age" value="22"/>
  8. <property name="addressInfo" ref="addressInfo"/>
  9. </bean>
  10. <bean id="addressInfo" class="com.haan.di.AddressInfo">
  11. <property name="tel" value="1008611"/>
  12. <property name="city" value="上海" />
  13. </bean>
  14. </beans>

(3)main函数测试

  1. public class Main {
  2. public static void main(String[] args) {
  3. ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
  4. Student2 student2 = context.getBean(Student2.class);
  5. System.out.println( student2.toString());
  6. }
  7. }

使用 p-namespace 实现 XML 配置

构造函数注入

当容器调用带有一组参数的类构造函数时,基于构造函数的 DI 就完成了,其中每个参数代表一个对其他类的依赖。

单个有参构造方法注入

  1. <bean id="person1" class="com.haan.di.Person">
  2. <constructor-arg name="name" value="libai"/>
  3. </bean>

type属性

Index属性

  1. public class Main {
  2. public static void main(String[] args) {
  3. ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
  4. Person person1 = (Person) context.getBean("person1");
  5. System.out.println("person1"+person1.toString());
  6. Person person2 = (Person) context.getBean("person2");
  7. System.out.println("person2"+person2.toString());
  8. Person person3 = (Person) context.getBean("person3");
  9. System.out.println("person3"+person3.toString());
  10. }
  11. }
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <bean id="person1" class="com.haan.di.Person">
  6. <constructor-arg name="name" value="libai"/>
  7. </bean>
  8. <bean id="person2" class="com.haan.di.Person">
  9. <constructor-arg type="java.lang.String" value="zhangfei"/>
  10. <constructor-arg type="java.lang.Integer" value="22"/>
  11. </bean>
  12. <bean id="person3" class="com.haan.di.Person">
  13. <constructor-arg index="0" value="zhangfei"/>
  14. <constructor-arg index="1" value="22"/>
  15. <constructor-arg index="2" value="1008611"/>
  16. </bean>
  17. <bean id="test1" class="com.haan.test.Test1" />
  18. <bean id="test2" class="com.haan.test.Test2" />
  19. </beans>
  1. public class Person {
  2. private String name;
  3. private Integer age;
  4. private String tel;
  5. public Person(){}
  6. public Person(String name){
  7. this.name = name;
  8. }
  9. public Person(String name, Integer age) {
  10. this.name = name;
  11. this.age = age;
  12. }
  13. public Person(String name, Integer age, String tel) {
  14. this.name = name;
  15. this.age = age;
  16. this.tel = tel;
  17. }
  18. @Override
  19. public String toString() {
  20. return "Person{" +
  21. "name='" + name + '\'' +
  22. ", age=" + age +
  23. ", tel='" + tel + '\'' +
  24. '}';
  25. }
  26. }
  1. public class Main {
  2. public static void main(String[] args) {
  3. ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
  4. Person person1 = (Person) context.getBean("person1");
  5. System.out.println("person1"+person1.toString());
  6. Person person2 = (Person) context.getBean("person2");
  7. System.out.println("person2"+person2.toString());
  8. Person person3 = (Person) context.getBean("person3");
  9. System.out.println("person3"+person3.toString());
  10. }
  11. //person1Person{name='libai', age=null, tel='null'}
  12. //person2Person{name='zhangfei', age=22, tel='null'}
  13. //person3Person{name='zhangfei', age=22, tel='1008611'}
  14. }

工厂方式注入