Spring DI简介

Spring DI简介

DI:依赖注入
概念:给属性赋值

  • 1、调用set方法
    基础类型
    引用类型
  • 2、调用构造器
  • 3、利用注解的形式
public class Student{

}    
public class Person{
  private Long pid;
  private String name;
  private Student student;
  private List list;
  private Set set;
  private Map map;
  private Properties properties;
}

步骤:

  • 1、创建一个Student类
  • 2、创建一个Person类
  • 3、创建一个spring的配置文件,把student类和person类 放入到spring容器中
  • 4、装配各个属性

执行流程:

  • 1、启动spring容器
  • 2、创建依赖对象(Student对象)
  • 3、创建Person对象
  • 4、给person对象中的属性进行装配的操作

    注意:
    如果把Student的配置方式改成scope=”prototype”,
    因为其他的类的创建和属性的装配的过程全部发生在spring容器启动的时候,所以Student对象的创建被提前了
    ,但是还是用一次即创建一次对象