spring4学习IOC之注入参数(四)

依赖注入之注入参数

这里写图片描述

1.编写People和Dog类

package com.newbeedaly.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class People {

    private int id;
    private String name;
    private int age;
    private Dog dog;
    private List<String> hobbies=new ArrayList<String>();
    private Set<String> loves=new HashSet<String>();
    private Map<String,String> works=new HashMap<String,String>();
    private Properties addresses=new Properties();

    public People() {
        super();
        // TODO Auto-generated constructor stub
    }




    public People(int id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }




    public Set<String> getLoves() {
        return loves;
    }




    public void setLoves(Set<String> loves) {
        this.loves = loves;
    }




    public List<String> getHobbies() {
        return hobbies;
    }




    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }




    public Map<String, String> getWorks() {
        return works;
    }




    public void setWorks(Map<String, String> works) {
        this.works = works;
    }




    public Properties getAddresses() {
        return addresses;
    }




    public void setAddresses(Properties addresses) {
        this.addresses = addresses;
    }




    public Dog getDog() {
        return dog;
    }




    public void setDog(Dog dog) {
        this.dog = dog;
    }




    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }




    @Override
    public String toString() {
        return "People [id=" + id + ", name=" + name + ", age=" + age
                + ", dog=" + dog + ", hobbies=" + hobbies + ", loves=" + loves
                + ", works=" + works + ", addresses=" + addresses + "]";
    }


}
package com.newbeedaly.entity;

public class Dog {

    private String name;

    public String getName() {
        return name;
    }

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


}

2.配置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="people1" class="com.newbeedaly.entity.People">
        <property name="id" value="1"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="11"></property>
    </bean>

    <bean id="dog1" class="com.newbeedaly.entity.Dog">
        <property name="name" value="Jack"></property>
    </bean>

    <bean id="people2" class="com.newbeedaly.entity.People">
        <property name="id" value="1"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="11"></property>
        <property name="dog" ref="dog1"></property>
    </bean>

    <bean id="people3" class="com.newbeedaly.entity.People">
        <property name="id" value="1"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="11"></property>
        <property name="dog">
            <bean class="com.newbeedaly.entity.Dog">
                <property name="name" value="Tom"></property>
            </bean>
        </property>
    </bean>

    <bean id="people4" class="com.newbeedaly.entity.People">
        <property name="id" value="1"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="11"></property>
        <property name="dog">
            <null></null>
        </property>
    </bean>

    <!-- <bean id="people5" class="com.newbeedaly.entity.People">
        <property name="id" value="1"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="11"></property>
        <property name="dog.name" value="Jack2"></property>
    </bean> -->

    <bean id="people6" class="com.newbeedaly.entity.People">
        <property name="id" value="1"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="11"></property>
        <property name="dog" ref="dog1"></property>
        <property name="hobbies">
            <list>
                <value>唱歌</value>
                <value>跳舞</value>
            </list>
        </property>
        <property name="loves">
            <set>
                <value>唱歌2</value>
                <value>跳舞2</value>
            </set>
        </property>
        <property name="works">
            <map>
                <entry>
                    <key><value>上午</value></key>
                    <value>写代码</value>
                </entry>
                <entry>
                    <key><value>下午</value></key>
                    <value>测试代码</value>
                </entry>
            </map>
        </property>
        <property name="addresses">
            <props>
                <prop key="address1">aaaaa</prop>
                <prop key="address2">bbbbb</prop>
            </props>
        </property>
    </bean>

</beans>

3.编写测试类

package com.newbeedaly.test;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.newbeedaly.entity.People;

public class T {

    private ApplicationContext ac;

    @Before
    public void setUp() throws Exception {
        ac=new ClassPathXmlApplicationContext("beans.xml");
    }

    // 基本类型值
    @Test
    public void test1() {
        People people=(People)ac.getBean("people1");
        System.out.println(people);
    }

    // 注入bean
    @Test
    public void test2() {
        People people=(People)ac.getBean("people2");
        System.out.println(people);
    }


    // 内部bean
    @Test
    public void test3() {
        People people=(People)ac.getBean("people3");
        System.out.println(people);
    }

    // 注入null
    @Test
    public void test4() {
        People people=(People)ac.getBean("people4");
        System.out.println(people);
    }

    // 级联属性
    @Test
    public void test5() {
        People people=(People)ac.getBean("people5");
        System.out.println(people);
    }

    // 注入集合
    @Test
    public void test6() {
        People people=(People)ac.getBean("people6");
        System.out.println(people);
    }
}

猜你喜欢

转载自blog.csdn.net/willdic/article/details/80536099