Spring中的集合的注入方式

jopen 8年前發布 | 7K 次閱讀 Spring JEE框架

上代碼  bean.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"  
            xmlns:context="http://www.springframework.org/schema/context"  
            xsi:schemaLocation="http://www.springframework.org/schema/beans  
                http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
                http://www.springframework.org/schema/context  
                http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
    
     <bean id="collectionBean" class="org.heinrich.CollectionBean">
         <property name="sets">
             <set>
                 <value>Heinrich</value>
                 <value>Reading Thinking Java</value>
             </set>
         </property>
         <property name="users">
             <list>
                 <ref bean="user1"/>
                 <ref bean="user2"/>
             </list>
         </property>
         <property name="maps">
             <map>
                 <entry key="BookName">
                     <value>Thinking Java</value>
                 </entry>
                 <entry key="PhoneName">
                     <value>Apple 6 plus</value>
                 </entry>
             </map>
         </property>
         <property name="properties">
             <props>
                 <prop key="1">MySQL</prop>
                 <prop key="2">Oracle</prop>
                 <prop key="3">SQLSERVER</prop>
             </props>
         </property>
     </bean>
    
    <bean id="user1" class="org.heinrich.User">
        <property name="name">
            <value>Heinrich</value>
        </property>
        <property name="age">
            <value>21</value>
        </property>
    </bean>   
    <bean id="user2" class="org.heinrich.User">
        <property name="name">
            <value>Zhangyanmei</value>
        </property>
        <property name="age">
            <value>22</value>
        </property>
    </bean> 
  </beans>
package org.heinrich;

public class User {

    private String name;
    private Integer age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }

}

上面是實體類User,下面是需要注入的實體類

package org.heinrich;

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




public class CollectionBean {


    private List<User> users;
    private Map<String,String> maps;
    private Set<String> sets;
    private Properties properties;
    public List<User> getUsers() {
        return users;
    }
    public void setUsers(List<User> users) {
        this.users = users;
    }
    public Map<String, String> getMaps() {
        return maps;
    }
    public void setMaps(Map<String, String> maps) {
        this.maps = maps;
    }
    public Set<String> getSets() {
        return sets;
    }
    public void setSets(Set<String> sets) {
        this.sets = sets;
    }
    public Properties getProperties() {
        return properties;
    }
    public void setProperties(Properties properties) {
        this.properties = properties;
    }

}

測試類

package org.heinrich;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BeanTest {

    public static void main(String[] args) {

        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
        User user1 = (User)ctx.getBean("user1");
        User user2 = (User)ctx.getBean("user1");
        System.out.println(user1);
        System.out.println(user2);
        CollectionBean collectionBean = (CollectionBean)ctx.getBean("collectionBean");
        System.out.println(collectionBean);
        System.out.println(collectionBean.getMaps());
        System.out.println(collectionBean.getSets());
        System.out.println(collectionBean.getUsers());
        System.out.println(collectionBean.getProperties());

    }

}

測試結果

org.heinrich.User@f8dd88b
org.heinrich.User@f8dd88b
org.heinrich.CollectionBean@298395a7
{BookName=Thinking Java, PhoneName=Apple 6 plus}
[Heinrich, Reading Thinking Java]
[org.heinrich.User@f8dd88b, org.heinrich.User@7dd61c3b]
{3=SQLSERVER, 2=Oracle, 1=MySQL}


來自: http://my.oschina.net/heinrichchen/blog/599759

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!