spring-list.set.map.props集合注入

集合注入

本文主要内容是演示如何进行list,set,map,props等集合注入,包括普通类型和对象

package com.picc.spring.depencyinjection;

public class SomeOne {
    private String name;
    private String age;

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "SomeOne{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                '}';
    }
}

package com.picc.spring.depencyinjection;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class SomeBean {
    private String sex;

    private List list;

    private Set set;

    private Map map;

    private Properties prop;

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    public Set getSet() {
        return set;
    }

    public void setSet(Set set) {
        this.set = set;
    }

    public Map getMap() {
        return map;
    }

    public void setMap(Map map) {
        this.map = map;
    }

    public Properties getProp() {
        return prop;
    }

    public void setProp(Properties prop) {
        this.prop = prop;
    }

    @Override
    public String toString() {
        return "SomeBean{" +
                "sex='" + sex + '\'' +
                ", list=" + list +
                ", set=" + set +
                ", map=" + map +
                ", prop=" + prop +
                '}';
    }
}
public class springBean {

  private static ClassPathXmlApplicationContext classPath;
  @Test
  public void testlist(){
    SomeBean apple = classPath.getBean("someBean", SomeBean.class);
    System.out.println(apple);
  }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="someOne" class="com.picc.spring.depencyinjection.SomeOne" p:age="12" p:name="xxx">
    </bean>
    <bean id="someBean" class="com.picc.spring.depencyinjection.SomeBean">
        <property name="sex" value="man"></property>
        <property name="list">
            <list>
                <ref bean="someOne"></ref>
                <value>111</value>
            </list>
        </property>
        <property name="set">
            <set>
                <ref bean="someOne"></ref>
                <value>444</value>
            </set>
        </property>
        <property name="map">
            <map>
                <entry key="aaa" value-ref="someOne"></entry>
                <entry key="bbb" value="666"></entry>
                <entry key-ref="someOne" value-ref="someOne"></entry>
            </map>
        </property>
        <property name="prop">
            <props>
                <prop key="a">123</prop>
                <prop key="b">456</prop>
            </props>
        </property>
    </bean>
</beans>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值