zoukankan      html  css  js  c++  java
  • spring为集合属性赋值{map,list,set,properties}

    第一步:新建工程   SecondSpring

    文件目录结构如下:

    第二步: 导入相关的spring jar包

    略...

    第三步: 新建类

    GatherTest.java

    package com.xuzhiwen.spring5;
    
    import java.util.List;
    import java.util.Map;
    import java.util.Properties;
    import java.util.Set;
    
    public class GatherTest {
        public List<Object> list;
        public List<Object> objlist;
        public Map<String,Object> map;
        public Set<Object> set;
        public Properties properties;
        
        public void setList(List<Object> list) {
            this.list = list;
        }
        
        public void setObjlist(List<Object> objlist) {
            this.objlist = objlist;
        }
        
        public void setMap(Map<String, Object> map) {
            this.map = map;
        }
        public void setSet(Set<Object> set) {
            this.set = set;
        }
        public void setProperties(Properties properties) {
            this.properties = properties;
        }
    
        @Override
        public String toString() {
            return "GatherTest [list=" + list +", objlist=" + objlist + ", map="
                    + map + ", set=" + set + ", properties=" + properties + "]";
        }
    }

    Person1.java

    package com.xuzhiwen.spring5;
    
    public class Person1 {
        public String name;
        
        public void setName(String name) {
            this.name = name;
        }
    
        @Override
        public String toString() {
            return "Person1 [name=" + name + "]";
        }
    }

    Person2.java

    package com.xuzhiwen.spring5;
    
    public class Person2 {
        public String name;
        
        public void setName(String name) {
            this.name = name;
        }
    
        @Override
        public String toString() {
            return "Person2 [name=" + name + "]";
        }
    }

    第四步: 新建配置文件

    common.xml

    <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-2.5.xsd">
        
        <import resource="xmlfolder/app1.xml" />
        <import resource="xmlfolder/innerbean.xml" />
        <import resource="xmlfolder/singleton.xml" />
        <import resource="xmlfolder/annotation.xml" />
        <import resource="xmlfolder/gather.xml" />
        
    </beans>    

    annotation.xml

    <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-2.5.xsd">
        
        <bean id="gather" class="com.xuzhiwen.spring5.GatherTest">
            <!-- 给list赋值 ======================================================-->
            <property name="list">
                <list>
                    <value>张三</value>
                    <value>李四</value>
                    <value>王五</value>
                </list>
            </property>
            
            <property name="objlist">
                <list>
                    <ref bean="person1" />
                    <ref bean="person2" />
                </list>
            </property>
            
            <!-- 给map赋值 ======================================================-->
            <property name="map">
                <map>
                    <entry key="key1" value="value1" />
                    <entry key="key2" value="value2" />
                    <entry key="key3" value="value3" />
                    
                    <entry key="key4" value-ref="person1" />
                    <entry key="key5" value-ref="person2" />
                </map>            
            </property>
            
            <!-- 给set赋值 ======================================================-->
            <property name="set">
                <set>
                    <ref local="person1" />
                    <ref local="person2" />
                </set>
            </property>
            
            <!-- 给properties赋值============================================= -->
            <property name="properties">
                <props>
                    <prop key="aaaa">aaaa</prop>
                    <prop key="bbbb">bbbb</prop>
                </props>
            </property>
        </bean>
        
        
        
        
        <!-- ====================================================== -->
        <!-- person1 -->
        <bean id="person1" class="com.xuzhiwen.spring5.Person1">
            <property name="name" value="xuzhiwen" />
        </bean>
        
        <!-- person2 -->
        <bean id="person2" class="com.xuzhiwen.spring5.Person2">
            <property name="name" value="fanjialing" />
        </bean>
        
    </beans>    

    第五步:新建测试类

    Test.java

    package com.xuzhiwen.spring5;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test {
        public static void main(String[] args) {
            ApplicationContext app = new ClassPathXmlApplicationContext("common.xml");
            GatherTest test = (GatherTest) app.getBean("gather");
            System.out.println(test);
        }
    }

    第六步:运行结果如下

    
     GatherTest [list=[张三, 李四, 王五],
    objlist=[Person1 [name=xuzhiwen], Person2 [name=fanjialing]],
    map={key1=value1, key2=value2, key3=value3, key4=Person1 [name=xuzhiwen], key5=Person2 [name=fanjialing]},
    set=[Person1 [name=xuzhiwen], Person2 [name=fanjialing]],
    properties={aaaa=aaaa, bbbb=bbbb}]
  • 相关阅读:
    (转)GNU风格ARM汇编语法指南(非常详细)5
    (转)ARM GNU常用汇编语言介绍
    RH850 FCL的使用
    RL78 FDL和FSL的使用
    RH850 中断相关
    关于const类型指针变量的使用
    Linux内核中双向链表的经典实现
    RL78 定义常量变量在指定的地址方法
    RL78 RAM GUARD Funtion
    AUTOSAR-Specification of Watchdog Manager 阅读
  • 原文地址:https://www.cnblogs.com/beibidewomen/p/7389989.html
Copyright © 2011-2022 走看看