Spring-依赖注入(DI)

什么是依赖注入?
在控制反转(IOC)创建对象的过程中,Spring通过配置信息向对象中设置属性的值。

两种方式:

  • 构造器注入(上一篇)
  • set注入

1.创建实体类,里面有一些属性等会需要进行注入

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 void setName(String name) {
    
    
        this.name = name;
    }

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

    public void setBooks(String[] books) {
    
    
        this.books = books;
    }

    public void setHobbys(List<String> hobbys) {
    
    
        this.hobbys = hobbys;
    }

    public void setCard(Map<String, String> card) {
    
    
        this.card = card;
    }

    public void setGames(Set<String> games) {
    
    
        this.games = games;
    }

    public void setWife(String wife) {
    
    
        this.wife = wife;
    }

    public void setInfo(Properties info) {
    
    
        this.info = info;
    }

    public void show(){
    
    
        System.out.println("name="+ name
                + ",address="+ address.getAddress()
                + ",books="
        );
        for (String book:books){
    
    
            System.out.print("<<"+book+">>\t");
        }
        System.out.println("\n爱好:"+hobbys);

        System.out.println("card:"+card);

        System.out.println("games:"+games);

        System.out.println("wife:"+wife);

        System.out.println("info:"+info);

    }
}

2.创建Spring配置文件beans.xml,标准名ApplicationContext.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

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

    <bean id="student" class="com.kuang.pojo.Student">
        <!--简单类型-->
        <property name="name" value="张三"/>
        <!--对象类型-->
        <property name="address" ref="address"/>
        <!--数组-->
        <property name="books">
            <array>
                <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="123456789"/>
                <entry key="手机号" value="15646516546"/>
            </map>
        </property>
        <!--set-->
        <property name="games">
            <set>
                <value>英雄联盟</value>
                <value>绝地求生</value>
            </set>
        </property>
        <!--null注入-->
        <property name="wife">
            <null/>
        </property>
        <!--properties注入-->
        <property name="info">
            <props>
                <prop key="key">value</prop>
            </props>
        </property>
    </bean>
    
</beans>

3.编写Junit测试

    @Test
    public void test(){
    
    
    	//加载配置文件,获取context容器
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        //从容器中获取id为student的bean对象
        Student bean = context.getBean("student", Student.class);
        bean.show();
    }

4.结果
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_42665745/article/details/112252405