DI依赖注入-Spring学习

6.DI依赖注入

6.1构造器注入

6.2set方式注入【重点】

.依赖注入:

。依赖:bean对象的创建依赖于set注入容器。

。注入:bean对象中所有属性,由容器来注入。

【环境搭建】

1.复杂类型

package com.kuang.pojo;

public class Address {
    private String address;
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }

}

2.真实测试对象

public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties info;
     public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    }

3.测试类

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());
    }
}

4.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.kuang.pojo.Student">
        <!--第一种 普通值注入 直接使用value-->
        <property name="name" value="陈旺山"></property>
    </bean>
</beans>

完善注入信息

<?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="address" class="com.kuang.pojo.Address">
        <property name="address" value="北京"></property>
    </bean>
    <bean id="student" class="com.kuang.pojo.Student">
        <!--第一种 普通值注入 直接使用value-->
        <property name="name" value="张三"></property>
        <!-- 第二种  bean注入  是一个引用  用ref-->
        <property name="address" ref="address"></property>

        <!--数组注入-->
        <property name="books">
            <array>
                <value>西游记</value>
                <value>水浒传</value>
                <value>三国演义</value>
                <value>红楼梦</value>
            </array>
        </property>
        <!--集合注入-->
        <property name="hobbys">
            <list>
                <value>打球</value>
                <value>敲代码</value>
            </list>
            <!--map注入-->
        </property>
        <property name="card">
            <map>
                <entry key="身份证" value="1234567890098"></entry>
            </map>
        </property>
        <!--set注入-->
        <property name="games">
            <set>
                <value>王者</value>
                <value>吃鸡</value>
            </set>
        </property>
        <!--null注入-->
        <property name="wife">
            <null></null>
        </property>
        <!--Properties-->
        <property name="info">
            <props>
                <prop key="学号">183206102</prop>
                <prop key="性别">男</prop>
            </props>
        </property>
    </bean>
</beans>
上一篇:.NET CORE DI 依赖注入


下一篇:flask_celery