DI

@Data
public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbies;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties properties;
}
    <bean id="student" class="com.lee.pojo.Student">
      <!--普通注入-->
        <property name="name" value="LeeJean"/>
      <!--Bean注入,使用ref-->
        <property name="address" ref="address"/>
      <!--数组注入-->
        <property name="books">
            <array>
                <value>aaa</value>
                <value>bbb</value>
                <value>ccc</value>
                <value>ddd</value>
            </array>
        </property>
      <!--list注入-->
        <property name="hobbies">
            <list>
                <value>111</value>
                <value>222</value>
                <value>333</value>
            </list>
        </property>
      <!--map注入-->
        <property name="card">
            <map>
                <entry key="a" value="aa"/>
                <entry key="b" value="bb"/>
                <entry key="c" value="cc"/>
            </map>
        </property>
      <!--set注入-->
        <property name="games">
            <set>
                <value>set1</value>
                <value>set2</value>
            </set>
        </property>
      <!--null注入-->
        <property name="wife">
            <null/>
        </property>
      <!--properties注入-->
        <property name="properties">
            <props>
                <prop key="num">1</prop>
                <prop key="age">22</prop>
                <prop key="sex">1</prop>
            </props>
        </property>
    </bean>

此外,还有p注入和c注入,其中p对应的是properties,即属性;c对应的constructor,即构造器

使用之前要在xml里加上约束,注意第三行,使用范例如下

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean name="john-classic" class="com.example.Person">
        <property name="name" value="John Doe"/>
        <property name="spouse" ref="jane"/>
    </bean>

    <bean name="john-modern"
        class="com.example.Person"
        p:name="John Doe"
        p:spouse-ref="jane"/>

    <bean name="jane" class="com.example.Person">
        <property name="name" value="Jane Doe"/>
    </bean>
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:c="http://www.springframework.org/schema/c"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="beanTwo" class="x.y.ThingTwo"/>
    <bean id="beanThree" class="x.y.ThingThree"/>

    <!-- traditional declaration with optional argument names -->
    <bean id="beanOne" class="x.y.ThingOne">
        <constructor-arg name="thingTwo" ref="beanTwo"/>
        <constructor-arg name="thingThree" ref="beanThree"/>
        <constructor-arg name="email" value="something@somewhere.com"/>
    </bean>

    <!-- c-namespace declaration with argument names -->
    <bean id="beanOne" class="x.y.ThingOne" c:thingTwo-ref="beanTwo"
        c:thingThree-ref="beanThree" c:email="something@somewhere.com"/>

</beans>

如果一个类A 的功能实现需要借助于类B,那么就称类B是类A的依赖,如果在类A的内部去实例化类B,那么两者之间会出现较高的耦合,一旦类B出现了问题,类A也需要进行改造,如果这样的情况较多,每个类之间都有很多依赖,那么就会出现牵一发而动全身的情况,程序会极难维护,并且很容易出现问题。要解决这个问题,就要把A类对B类的控制权抽离出来,交给一个第三方去做,把控制权反转给第三方,就称作控制反转(IOC Inversion Of Control)。控制反转是一种思想,是能够解决问题的一种可能的结果,而依赖注入(Dependency Injection)就是其最典型的实现方法。由第三方(我们称作IOC容器)来控制依赖,把他通过构造函数、属性或者工厂模式等方法,注入到类A内,这样就极大程度的对类A和类B进行了解耦。

原文地址:https://www.cnblogs.com/tudoo/p/12845374.html