【Spring实战】—— 7 复杂集合类型的注入

之前讲解了Spring的基本类型和bean引用的注入,接下来学习一下复杂集合类型的注入,例如:List、Set、Map等。

  对于程序员来说,掌握多种语言是基本的技能。

  我们这里做了一个小例子,程序员们是一个Bean实例,而掌握的编程语言则是放入一个集合类型中进行注入。

  简单的来说:

  List是一种按照序号标识的集合,

  Set与List相似但是元素不允许重复,

  Map则是一种自定的键值对,键值都可以是任意的类型。

  Bean的实现类

public class Programmer {
    public Programmer(){}
    //List 与 Set的注入
    private Collection<Language> language;
    public void setLanguage(Collection<Language> language){
        this.language = language;
    }
    public void working(){
        for(Language lang : language){
            System.out.println("working on "+lang.using());
        }
    }
    //Map的注入
    private Map<String,Language> lang;
    public void setLang(Map<String,Language> lang){
        this.lang = lang;
    }
    public void studying(){
        for(String key : lang.keySet()){
            System.out.print(key+" : ");
            Language language = lang.get(key);
            System.out.println("studying on "+language.using());
        }
    }
}

  其他需要的一些接口和实现类

public interface Language {
    public String using();
}
View Code
public class Java implements Language{

    public String using() {
        return "Java";
    }
    
}
View Code
public class Cpp implements Language{
    public String using() {
        return "C++";
    }
}
View Code

  Spring的配置文件如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <bean id="java" class="com.spring.test.collector.Java"/>
    <bean id="cpp" class="com.spring.test.collector.Cpp"/>
    <bean id="xingoo" class="com.spring.test.collector.Programmer">
        <property name="language">
            <list>
                <ref bean="java"/>
                <ref bean="cpp"/>
            </list>
        </property>
    </bean>
    <bean id="halo" class="com.spring.test.collector.Programmer">
        <property name="language">
            <set>
                <ref bean="java"/>
            </set>
        </property>
    </bean>
    <bean id="zhang3" class="com.spring.test.collector.Programmer">
        <property name="lang">
            <map>
                <!-- key键为String key-ref键为其他bean的引用 -->
                <!-- value值为String value-ref值为其他bean的引用 -->
                <entry key="java" value-ref="java"/>
                <entry key="cpp" value-ref="cpp"/>
            </map>
        </property>
    </bean>
</beans>

  获取Bean实例并使用

public class test {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
        System.out.println("[xingoo]");
        Programmer programmer = (Programmer)ctx.getBean("xingoo");
        programmer.working();
        System.out.println("[halo]");
        Programmer programmer1 = (Programmer)ctx.getBean("halo");
        programmer1.working();
        System.out.println("[zhang3]");
        Programmer programmer2 = (Programmer)ctx.getBean("zhang3");
        programmer2.studying();
    }
}

  执行结果:

[xingoo]
working on Java
working on C++
[halo]
working on Java
[zhang3]
java : studying on Java
cpp : studying on C++

 

posted @ 2015-01-25 21:50  xingoo  阅读(4606)  评论(1编辑  收藏  举报