首页 > 技术文章 > Spring 依赖注入(DI) 的三种方式 和 对集合类型的注入

DDiamondd 原文

// 分别省略了getter  setter
public class Student {
    private String name;
    private int age;
    private Teacher teacher;
}

public class Teacher {
    private String tno;
    private String name;
}
<bean id="teacher" class="com.DO.Teacher">
    <property name="tno" value="123"></property>
    <property name="name" value="ls"></property>
</bean>

1、set方式注入(利用反射):

<bean id="student" class="com.DO.Student">   //调用类的无参构造器
    <property name="name" value="zxf"></property>  // 调用对应属性的set方法
    <!-- <null/>  给属性赋 null
    <property name="name">
            <null/>
    </property>  -->
    <property name="age">
          <value>22</value>
    </property>
    <property name="teacher" ref="teacher"></property>
</bean>

注意:

2、通过构造器注入:

<bean id="student" class="com.DO.Student">
    <!-- 根据参数的个数 会去调用相应的构造器   也可以通过索引指定 index 从0开始 也可以通过name指定参数名  或者指定参数的类型type -->
    <constructor-arg value="zs"></constructor-arg>
    <constructor-arg value="23"></constructor-arg>
    <constructor-arg ref="teacher"></constructor-arg>
</bean>

3、p命名空间:

引入p命名空间:

xmlns:p="http://www.springframework.org/schema/p"

<bean id="student" class="com.DO.Student" p:name="zs" p:age="22" p:teacher-ref="teacher"></bean>

自动装配:

byName:属性 与 bean的id 相同就自动装配

byType:属性 与 bean的类型(class="") 相同就自动装配 

可以把所有的bean都设置成自动装配: 在命名空间里添加: default-autowire="defalut"

自动装配会减少代码量,但是胡降低可读性

<bean id="student" class="com.zxf.DO.Student" autowire="byName">
    <property name="name" value="zs"></property>
    <property name="age" value="22"></property>
    <!-- 自动装配: 如果 该bean的属性 与 某个bean的id相同就会自动装配   student的teacher属性 与 id="teacher" 相匹配
        byName 的本质是 byId
      -->
</bean>

SpringIOC容器负责创建Bean

依赖注入:set方式注入: 把属性值注入给属性,把属性注入给对象。

                  通过构造器注入:调用相应的构造器,然后把value值注入进去。

                  p命名空间: 即使用p命名空间注入属性值。

控制反转:反转了用户获取对象的方式,从new (创建) --> get(直接拿)

 对集合类型的注入:

// 省略了对应的getter setter
public class ALLCollection {
    private List listElement;
    private String[] arrayElement;
    private Set setElement;
    private Map mapElement;
    private Properties propsElement;
}
<bean id="collection" class="com.zxf.DO.ALLCollection">
    <property name="listElement">
        <list>
            <value>list苹果</value>
            <value>list香蕉</value>
        </list>
    </property>
    <property name="arrayElement">
        <array>
            <value>array苹果</value>
            <value>array香蕉</value>
        </array>
    </property>
    <property name="setElement">
        <set>
            <value>set苹果</value>
            <value>set香蕉</value>
        </set>
    </property>
    <property name="mapElement">
        <map>
            <entry>
                <key><value>map1</value></key>
                <value>map苹果</value>
            </entry>
            <entry>
                <key><value>map2</value></key>
                <value>map香蕉</value>
            </entry>
        </map>
    </property>
    <property name="propsElement">
        <props>
            <prop key="prop1">prop苹果</prop>
            <prop key="porp2">prop香蕉</prop>
        </props>
    </property>
</bean>

推荐阅读