美文网首页
Spring学习之路(三)

Spring学习之路(三)

作者: 秋灯锁忆 | 来源:发表于2017-07-19 16:27 被阅读0次

    数据注入

    • 创建包含所有集合类别的类
    package com.collection;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    public class Deparment {
        private String name;
    //单个字符串属性
        private String[] empname;
    //array类型属性
        private List<Employee> emplists;
    //list类型属性
        private Set<Employee> empsets;
    //set类型属性
        private Map<String,Employee> empmaps;
    //map类型属性
        private Properties empporps;
    //Properties类型属性    
        public Properties getEmpporps() {
            return empporps;
        }
        public void setEmpporps(Properties empporps) {
            this.empporps = empporps;
        }
        public Set<Employee> getEmpsets() {
            return empsets;
        }
        public List<Employee> getEmplists() {
            return emplists;
        }
        public void setEmplists(List<Employee> emplists) {
            this.emplists = emplists;
        }
        public Map<String, Employee> getEmpmaps() {
            return empmaps;
        }
        public void setEmpmaps(Map<String, Employee> empmaps) {
            this.empmaps = empmaps;
        }
        public void setEmpsets(Set<Employee> empsets) {
            this.empsets = empsets;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String[] getEmpname() {
            return empname;
        }
        public void setEmpname(String[] empname) {
            this.empname = empname;
        }   
    }
    
    • 配置applicationContext.xml文件,引入该对象
    <bean id="deparment" class="com.collection.Deparment" >
    //中间为值配置
    </bean>
    
    • 单值注入
    <property name="name"  value="人事" />
    
    • array注入
    <property name="empname" >
        <list>  
            <value>小明</value>
            <value>大明</value>
        </list>
    </property>
    
    • list注入
    <-- ref指关联的bean对象 -->
    <--  list注入有序且允许值重复 -->
    <property name="emplists" >
        <list>
            <ref bean="emp1"/>
            <ref bean="emp2"/>
        </list>
    </property>
    
    • set注入
    <--  set注入无序且相同值会被覆盖 -->
    <property name="empsets" >
        <set>
            <ref bean="emp1"/>
            <ref bean="emp2"/>
        </set>
    </property>
    
    • map注入
    <--  mapt注入采用键值对的形式,要保证Key不可重复 -->
    <--  该entry内属性key,value视情况而选择对应属性表达 -->
    <property name="empmaps" >
        <map>
            <entry key="1" value-ref="emp1" />
            <entry key="2" value-ref="emp2" />
        </map>
    </property>
    
    • properties注入
    <property name="empporps" >
        <props>
            <prop key="1">abcd</prop>
            <prop key="2">abcf</prop>
        </props>
    </property>
    
    • Test测试类取出注入的值
    package com.collection;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Map.Entry;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class Test {
        public static void main(String[] args) {
        ApplicationContext ac=new ClassPathXmlApplicationContext("com/collection/beans.xml");
        Deparment deparment=(Deparment) ac.getBean("deparment");
    
        System.out.println(deparment.getName());
    //取出单值
        System.out.println("***************array***************");
        for(String empname:deparment.getEmpname()){
            System.out.println(empname);
        }
    //取出数组
        System.out.println("***************list***************");
        for(Employee e:deparment.getEmplists()){
            System.out.println(e.getName());
        }
    //取出list    
        System.out.println("***************set***************");
        for(Employee e:deparment.getEmpsets()){
            System.out.println(e.getName());
        }
    //取出set
        System.out.println("***************map***************");
        
        System.out.println("map方法");
        for(Entry<String,Employee> e:deparment.getEmpmaps().entrySet()){
            System.out.println(e.getKey()+""+e.getValue().getName());
        }   
        System.out.println("map迭代");
        Map<String,Employee> emp= deparment.getEmpmaps();
        Iterator<String> it=emp.keySet().iterator();
        while(it.hasNext()){
            String key=(String) it.next();
            Employee employee=emp.get(key);
            System.out.println(key+""+employee.getName());  
        }
    //取出map
        System.out.println("***************properties***************");
        System.out.println(deparment.getEmpporps().get("1").toString());
    //取出properties
        }
    }
    

    相关文章

      网友评论

          本文标题:Spring学习之路(三)

          本文链接:https://www.haomeiwen.com/subject/tngckxtx.html