spring ioc---参数注入

注入参数:

1,基本类型值;
2,注入bean;
3,内部bean;
4,null 值;
5,级联属性;
6,集合类型属性;

package entity;

public class Dog {
    private String name;

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

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

import 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);
    }
}
<?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="entity.People">
        <property name="id" value="1"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="11"></property>
    </bean>
    
    <bean id="dog1" class="entity.Dog">
        <property name="name" value="Jack"></property>
    </bean>
    
    <bean id="people2" class="entity.People">
        <property name="id" value="1"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="11"></property>
        <!-- 注入bean,就是嵌套另外一个bean -->
        <property name="dog" ref="dog1"></property>
    </bean>
    
    <bean id="people3" class="entity.People">
        <property name="id" value="1"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="11"></property>
        <property name="dog">
            <!-- 内部bean -->
            <bean class="entity.Dog">
                <property name="name" value="Tom"></property>
            </bean>
        </property>
    </bean>
    
    <bean id="people4" class="entity.People">
        <property name="id" value="1"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="11"></property>
        <property name="dog">
        <!-- 注入null -->
            <null></null>
        </property>
    </bean>
    
    <!-- <bean id="people5" class="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="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集合 -->
            <list>
                <value>唱歌</value>
                <value>跳舞</value>
            </list>
        </property>
        <property name="loves">
        <!-- set集合 -->
            <set>
                <value>唱歌2</value>
                <value>跳舞2</value>
            </set>
        </property>
        <property name="works">
        <!-- map集合 -->
            <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>

 

转载于:https://www.cnblogs.com/lcpholdon/p/4508527.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值