Spring Study -lesson05-DI依赖注入 -2023-03-15

DI依赖注入 :set注入

依赖:bean对象的创建依赖于容器

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

复杂类型注入:Address Student两个类

public class Address {
    private String address;
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 ifo;

 beans.xml 各种注入的方式 除了value 和ref注入两种方式之外  例如:数组、List、Map、Set、等

<?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-2.5.xsd">

    <bean id="address" class="com.feijian.pojo.Address">
        <property name="address" value="南京"/>
    </bean>

    <bean id="student" class="com.feijian.pojo.Student">
        <!--第一种,普通值注入 value-->
        <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="1234567812345678"/>
                <entry key="银行卡" value="600002888888"/>
            </map>
        </property>

        <!--第六种,Set注入-->
        <property name="games">
            <set>
                <value>LOL</value>
                <value>COC</value>
                <value>BOB</value>
            </set>
        </property>

        <!--第七种,null注入-->
        <property name="wife">
            <null/>
        </property>

        <!--第八种,Properties注入-->
        <property name="ifo">
            <props>
                <prop key="学号">20190526</prop>
                <prop key="性别">男</prop>
                <prop key="姓名">小明</prop>
            </props>
        </property>
    </bean>
</beans>

测试结果

import com.feijian.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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.toString());

        /*
        Student {
        name='飞剑',
        address=Address{address='南京'},
        books=[红楼梦, 西游记, 水浒传, 三国演义],
        hobbys=[听歌, 敲代码, 看电影],
        card={身份证=1234567812345678, 银行卡=600002888888},
        games=[LOL, COC, BOB],
        wife='null',
        ifo={学号=20190526, 性别=男, 姓名=小明}}
         */
    }
}

 

posted @ 2023-03-15 07:24  Rui2022  阅读(8)  评论(0编辑  收藏  举报