Spring 注入集合和数组

Spring 注入集合和数组,第1张

Spring 注入集合和数组
1、创建类、声明数组集合(List、Map、Set)

public class Stu {

    private String[] arrays;
    private List<String> list;
    private Map<String,String> map;
    private Set<String> set;

    public void setArrays(String[] arrays) {
        this.arrays = arrays;
    }

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

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

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

    @Override
    public String toString() {
        return "Stu{" +
                "arrays=" + Arrays.toString(arrays) +
                ", list=" + list +
                ", map=" + map +
                ", set=" + set +
                '}';
    }
}

2、配置文件中注入属性

 <bean id="stu" class="com.qxf.java.spring5.Stu">
        <property name="arrays">
            <array>
                <value>qqvalue>
                <value>yyvalue>
            array>
        property>
        <property name="list">
            <list>
                <value>mxvalue>
                <value>qxfvalue>
            list>
        property>
        <property name="map">
            <map>
                <entry key="js" value="JS">entry>
                <entry key="c#" value="C#">entry>
            map>
        property>
        <property name="set">
            <set>
                <value>cvalue>
                <value>javavalue>
            set>
        property>
    bean>

3.测试

 @Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        Stu stu = context.getBean("stu", Stu.class);
        System.out.println(stu.toString());
    }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/733713.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-27
下一篇 2022-04-27

发表评论

登录后才能评论

评论列表(0条)

保存