Spring's DI Dependency Injection

Spring's DI Dependency Injection

What is Dependency Injection

DI (Dependency Injection)

When the program runs, let spring provide the dependencies between layers. When creating containers, maintain the relationship between them for us. In use, we only need to provide the constructor or set method. Simply put, what is missing, what is passed in.

Constructor injection

  • bean object
/**
* Bean对象
*/
public class ConstructorDIDaoImpl implements ICustomerDao {

    private int id;
    private String name;
    private int age;
    private Date birthday;

    /**
    * 构造方法注入
    */
    public ConstructorDIDaoImpl(int id, String name, int age, Date birthday) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }
}
  • Constructor -arg tag : Specify to call the constructor to instantiate the object, initialize the object

  • Properties :

    • index: specifies the parameter, the index in the constructor parameter list
    • name: Specify the parameter, the name in the constructor parameter list (just use one with index)
    • type: Specify the parameter type (generally not used)
    • value: assign values ​​to parameters, for parameters of eight data types
    • ref: reference to parameter assignment, for other bean types
  • configuration file

<!--配置构造方法注入的对象,说明:
constructor-arg标签:指定调用构造方法实例化对象,初始化对象
    属性:
        index:指定参数,在构造方法参数列表中的索引
        name:指定参数,在构造方法参数列表中的名称(与index使用一个即可)
        type:指定参数类型(一般不用,有时候配置了spring也有时候会认错,比如Integer不能自动封箱拆箱)
        value:给参数赋值,针对简单类型的参数(八种基本类型+字符串)
        ref:给参数赋值,针对其它bean类型
-->
<bean id="constructoryDao" class="com.dao.impl.ConstructorDlDaoImpl">
    <constructor-arg index="0" type="int" value="1"></constructor-arg>
    <constructor-arg name="1" value="小明"></constructor-arg>
    <constructor-arg name="age" value="18"></constructor-arg>
    <constructor-arg name="birthday" ref="now"></constructor-arg>
</bean>
<bean id="now" class="java.util.Date"></bean>

Set method injection

Let spring use the set method in the class to assign values ​​to member variables.

  • bean object
public class ConstructorDIDaoImpl implements ICustomerDao {

    private int id;
    private String name;
    private int age;
    private Date birthday;

    get/set()......

}
  • property tag : specify to call the set method to assign values ​​to member variables
  • Properties :

    • name: the name of the assigned member variable
    • value: assign value to a variable of java simple type
    • ref: assign values ​​to other bean types
  • configuration file

<!-- set方法注入,说明:在实际项目中,set注入的方式使用更多
    property标签:指定调用set方法给成员变量赋值
        属性:
            name:赋值的成员变量的名称
            value:给java简单类型的变量赋值
            ref:给其它bean类型赋值
 -->
<bean id="setDao" class="cn.dao.impl.SetDependencyDaoImpl">
    <property name="id" value="1"></property>
    <property name="name" value="Hello"></property>
    <property name="age" value="18"></property>
    <property name="birthday" ref="now"></property>
</bean>
<bean id="now" class="java.util.Date"></bean>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325625512&siteId=291194637