[译]14-spring 集合元素的注入

前面的文章已经介绍了如何往bean里面注入原始类型和引用类型.我们使用bean元素的contructor-arg或property子

元素的value属性注入java原始类型;同理,我们可以使用bean元素的contructor-arg或property子元素的ref属性注入

java的引用类型.

 

但是至此为止,我们讲到的时候单个对象或原始值的注入.本节我们来讲解如何往bean中注入集合对象.spring中bean

元素的property和contructor-arg子元素可以包含以下四个子元素,用于集合类型的注入

元素 描述
<list> 用于注入List,允许有重复数据
<set> 用于注入set,不允许有重复数据
<map> 用于注入map映射
<props> 用于注入java Properties

 

 

 

 

上述四个元素既可以用来注入原始类型也可以用来注入引用类型.

这节内容其实很简单,下面直接上个例子.仔细看一遍例子程序,就能完全掌握集合元素的注入了.

 

1.创建包com.tutorialspoint.collection,并在包中新建InjectCollection.java、Hobby.java、Person.java,内容如下

InjectCollection.java如下:

package com.tutorialspoint.collection;

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

@SuppressWarnings("all")
public class InjectCollection {
    
    //用于演示包含原始数据的List的注入
    private List<String> list_hobby_pri;
    //用于演示包含引用类型的List的注入
    private List<Hobby> list_hobby_ref;
    //用于演示包含原始数据的Set的注入
    private Set<String> set_hobby_pri;
    //用于演示包含引用类型的Set的注入
    private Set<Hobby> set_hobby_ref;
    //用于演示包含原始数据的Map的注入
    private Map<String,String> map_hobby_pri;
    //用于演示包含引用数据的Map的注入
    private Map<Person,Hobby> map_hobby_ref;
    //用于演示property的注入
    private Properties prop_hobby;
    
    public void setList_hobby_pri(List<String> list_hobby_pri) {
        this.list_hobby_pri = list_hobby_pri;
    }
    public void setList_hobby_ref(List<Hobby> list_hobby_ref) {
        this.list_hobby_ref = list_hobby_ref;
    }
    public void setSet_hobby_pri(Set<String> set_hobby_pri) {
        this.set_hobby_pri = set_hobby_pri;
    }
    public void setSet_hobby_ref(Set<Hobby> set_hobby_ref) {
        this.set_hobby_ref = set_hobby_ref;
    }
    public void setMap_hobby_pri(Map<String, String> map_hobby_pri) {
        this.map_hobby_pri = map_hobby_pri;
    }
    public void setMap_hobby_ref(Map<Person, Hobby> map_hobby_ref) {
        this.map_hobby_ref = map_hobby_ref;
    }
    public void setProp_hobby(Properties prop_hobby) {
        this.prop_hobby = prop_hobby;
    }
    
    public void print(){
        System.out.println("list_hobby_pri: "+list_hobby_pri);
        System.out.println("list_hobby_ref: "+list_hobby_ref);
        System.out.println("set_hobby_pri: "+set_hobby_pri);
        System.out.println("set_hobby_ref: "+set_hobby_ref);
        System.out.println("map_hobby_pri: "+map_hobby_pri);
        System.out.println("map_hobby_ref: "+map_hobby_ref);
        System.out.println("prop_hobby: "+prop_hobby);
    }
}

 

Hobby.java如下:

package com.tutorialspoint.collection;

public class Hobby {

}

 

Person.java 如下:

package com.tutorialspoint.collection;

public class Person {

}

2.在src目录下新建di_collection.xml文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?>

<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-3.0.xsd">
   
   <!-- 定义三个Hobby实例 -->
   <bean id="hobby1" class="com.tutorialspoint.collection.Hobby"></bean>
   <bean id="hobby2" class="com.tutorialspoint.collection.Hobby"></bean>
   <bean id="hobby3" class="com.tutorialspoint.collection.Hobby"></bean>
   
   <!-- 定义三个Person实例 -->
   <bean id="person1" class="com.tutorialspoint.collection.Person"></bean>
   <bean id="person2" class="com.tutorialspoint.collection.Person"></bean>
   <bean id="person3" class="com.tutorialspoint.collection.Person"></bean>
   
   <!-- 下面示例了使用内部bean的方式注入spellChecker依赖项 -->
   <bean id="injectCollection" class="com.tutorialspoint.collection.InjectCollection">
   
        <!-- 包含原始类型List元素的注入 使用list元素的value子元素即可 -->
        <property name="list_hobby_pri">
           <list>
               <value>basketball_list</value>
               <value>reading_list</value>
               <value>surfing_list</value>
           </list>
        </property>
        
        <!-- 包含引用类型的List元素的注入使用list元素的ref子元素即可 -->
         <property name="list_hobby_ref">
           <list>
               <ref bean="hobby1"/>
               <ref bean="hobby2"/>
               <ref bean="hobby3"/>
           </list>
        </property>
        
        <!-- 包含原始类型的Set元素的注入使用set元素的value子元素即可 -->
        <property name="set_hobby_pri">
              <set>
                 <value>basketball_set</value>
                 <value>reading_set</value>
                 <value>surfing_set</value>
              </set>
        </property>
        
        <!-- 包含引用类型的Set元素的注入使用set元素的ref子元素即可 -->
        <property name="set_hobby_ref">
            <set>
                <ref bean="hobby1"/>
                <ref bean="hobby2"/>
                <ref bean="hobby3"/>
            </set>
        </property>
        
        <!-- 包含原始类型的Map元素的注入使用map元素的entry子元素的key和value属性即可 -->
        <property name="map_hobby_pri">
           <map>
             <entry key="person1_map" value="hobby1_map"></entry>
             <entry key="person2_map" value="hobby2_map"></entry>
             <entry key="person3_map" value="hobby3_map"></entry>
           </map>
        </property>
        
        <!-- 包含引用类型的Map元素的注入使用map元素的entry子元素的key-ref和value-ref属性即可 -->
        <property name="map_hobby_ref">
            <map>
              <entry key-ref="person1"  value-ref="hobby1"></entry>
              <entry key-ref="person2"  value-ref="hobby2"></entry>
              <entry key-ref="person3"  value-ref="hobby3"></entry>
            </map>
        </property>
        
        <!-- properties元素的注入 -->
        <property name="prop_hobby">
            <props>
               <prop key="person1_prop">hobby1_prop</prop>
               <prop key="person2_prop">hobby2_prop</prop>
               <prop key="person3_prop">hobby3_prop</prop>
            </props>
        </property>
        
   </bean>

</beans>

 

 3.在包com.tutorialspoint.collection中新建MainApp.java,内容如下:

package com.tutorialspoint.collection;

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

public class MainApp {

    public static void main(String[] args) {
        
        ApplicationContext context=new ClassPathXmlApplicationContext("di_collection.xml");
        InjectCollection ic = (InjectCollection)context.getBean("injectCollection");
        ic.print();
    }
}

 

4.运行程序,检查结果.

posted @ 2015-05-07 09:34  sysman  阅读(329)  评论(0编辑  收藏  举报