码迷,mamicode.com
首页 > 编程语言 > 详细

spring 注入集合类型 List、Set、Map

时间:2016-09-25 22:17:38      阅读:307      评论:0      收藏:0      [点我收藏+]

标签:

首先先看用到的类:

Class1,Class2

代码:

package my.spring.fuck;

public class Class1 implements Interface1{
    private String a;
    
    public String getA() {
        return a;
    }

    public void setA(String a) {
        this.a = a;
    }
    public void eat(String s){
        System.out.println(s+a);
    }
}
package my.spring.fuck;

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

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Class2 {
    private List<Class1> fuckList1;//集合统一装入Class1
    private List fuckList2;
    
    private Set<Class1> fuckSet1;
    private Set fuckSet2;
    
    private Map<String,String> fuckMap1;
    private Map<String,Class1> fuckMap2;
    
    public static void main(String[] args){
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        //Class2 c2=(Class2)ac.getBean("c2");
        Class2 c2=ac.getBean(Class2.class);
        c2.print();
    }
    
    public void print(){
        System.out.println("this is fuckList1");
        for(Class1 c1:fuckList1){
            c1.eat("");
        }
        
        System.out.println("this is fuckList2");
        for(int i=0;i<fuckList2.size();i++){
            ((Class1)fuckList2.get(i)).eat("");
        }
        
        System.out.println("this is fuckSet1");
        for(Class1 c1:fuckSet1){
            c1.eat("");
        }
        
        System.out.println("this is fuckSet2");
        Iterator<Class1> iterator=fuckSet2.iterator();
        while(iterator.hasNext()){
            (iterator.next()).eat("");
        }
        
        System.out.println("this is fuckMap1");
        System.out.println(fuckMap1.get("mapKey1"));
        
        System.out.println("this is fuckMap2");
        Class1 c1=fuckMap2.get("mapKey1");
        c1.eat("");
    }

    public List<Class1> getFuckList1() {
        return fuckList1;
    }

    public void setFuckList1(List<Class1> fuckList1) {
        this.fuckList1 = fuckList1;
    }

    public List getFuckList2() {
        return fuckList2;
    }

    public void setFuckList2(List fuckList2) {
        this.fuckList2 = fuckList2;
    }

    public Set<Class1> getFuckSet1() {
        return fuckSet1;
    }

    public void setFuckSet1(Set<Class1> fuckSet1) {
        this.fuckSet1 = fuckSet1;
    }

    public Set getFuckSet2() {
        return fuckSet2;
    }

    public void setFuckSet2(Set fuckSet2) {
        this.fuckSet2 = fuckSet2;
    }

    public Map<String, String> getFuckMap1() {
        return fuckMap1;
    }

    public void setFuckMap1(Map<String, String> fuckMap1) {
        this.fuckMap1 = fuckMap1;
    }

    public Map<String, Class1> getFuckMap2() {
        return fuckMap2;
    }

    public void setFuckMap2(Map<String, Class1> fuckMap2) {
        this.fuckMap2 = fuckMap2;
    }
    
    
}

application.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
    <bean id="c2" class="my.spring.fuck.Class2">
        <property name="fuckList1">
            <list>
                <ref bean="c1"/>
                <ref bean="c1"/>
            </list>
        </property>
        <property name="fuckList2">
            <list>
                <ref bean="c1"/>
                <ref bean="c1"/>
            </list>
        </property>
        <property name="fuckSet1">
            <set>
                <ref bean="c1"/>
                <ref bean="c1"/>
            </set>
        </property>
        <property name="fuckSet2">
            <set>
                <ref bean="c1"/>
                <ref bean="c1"/>
            </set>
        </property>
        <property name="fuckMap1">
            <map>
                <entry key="mapKey1" value="mapValue1"/>
            </map>
        </property>
        <property name="fuckMap2">
            <map>
                <entry key="mapKey1">
                    <ref bean="c1"/>
                </entry>
            </map>
        </property>
    </bean>
    <bean id="c1" class="my.spring.fuck.Class1">
        <property name="a" value="i am a">
        </property>
    </bean>
</beans>

运行Class2,控制台输出如下:

this is fuckList1
i am a
i am a
this is fuckList2
i am a
i am a
this is fuckSet1
i am a
this is fuckSet2
i am a
this is fuckMap1
mapValue1
this is fuckMap2
i am a

spring 注入集合类型 List、Set、Map

标签:

原文地址:http://www.cnblogs.com/juson37/p/5907059.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!