DI依赖注入

1.构造器注入:已经了解过

2.Set方式注入:
依赖注入:Set注入
依赖:bean对象的创建依赖于容器
注入:bean对象中的所有属性,由容器来注入

环境搭建:
复杂类型

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

真实测试对象

 private String name;
    private Address address;
    private String[] books;
    private List<String > hobbys;
    private Map<String ,String> card;
    private Set<String> games;
    private Properties info;
    private String wife;

beans.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="student" class="com.dai.pojo.Student">
        <property name="name" value="戴"/>
    </bean>

</beans>

测试类:

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.getName());

    }
}

完善注入信息:

  <bean id="address" class="com.dai.pojo.Address"/>

    <bean id="student" class="com.dai.pojo.Student">
        <property name="name" value="戴"/>

<!--        第二种,Bean注入,ref-->
        <property name="address" ref="address"/>

<!--        数组注入-->
        <property name="books">
            <array>
                <value>三国</value>
                <value>红楼梦</value>
                <value>水浒传</value>
                <value>西游记</value>
            </array>
        </property>

<!--        List注入-->
        <property name="hobbys">
            <list>
                <value>听歌</value>
                <value>写作业</value>
                <value>打游戏</value>
            </list>
        </property>

<!--        Map注入-->
        <property name="card">
            <map>
                <entry key="身份证" value="1111"/>
                <entry key="银行卡" value="2222"/>
                <entry key="公交卡" value="3333"/>
            </map>
        </property>

<!--        set注入-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>CS</value>
            </set>
        </property>

<!--        null-->
        <property name="wife">
            <null></null>
        </property>

<!--        Properties-->
        <property name="info">
            <props>
                <prop key="学号">11</prop>
                <prop key="性别">男</prop>
            </props>
        </property>

    </bean>

bean的作用域:
1.单例模式(Spring默认机制)

    <bean id="student" class="com.dai.pojo.Student" 
scope="singleton">

2.原型模式:每次从容器中get的时候,都会产生一个新对象

<bean id="student" class="com.dai.pojo.Student"
 scope="prototype">

3.其余的request、session、application这些只能在web开发中使用到

上一篇:【MySQL】事件调度器 (Event Scheduler)


下一篇:Scan plan阶段