Spring中property的list属性注入

来源:互联网 发布:金融软件行业 编辑:程序博客网 时间:2024/05/01 01:25

用数值配置spring装配的JavaBean内部的List类型很容易,下面介绍如何用javabean装配JavaBean中的List

Java代码

[java] view plain copy
  1. public class Element implements Serializable{  
  2.     /** 
  3.      * 
  4.      */  
  5.     private static final long serialVersionUID = -6956332143541075576L;  
  6.     private Integer id;  
  7.     private String name;  
  8.     private String url;  
  9.     public Integer getId() {  
  10.         return id;  
  11.     }  
  12.     public void setId(Integer id) {  
  13.         this.id = id;  
  14.     }  
  15.     public String getName() {  
  16.         return name;  
  17.     }  
  18.     public void setName(String name) {  
  19.         this.name = name;  
  20.     }  
  21.     public String getUrl() {  
  22.         return url;  
  23.     }  
  24.     public void setUrl(String url) {  
  25.         this.url = url;  
  26.     }  
  27.       
  28. }  

 

public class Test {

[java] view plain copy
  1. private List<Element> elementList;  
  2.   
  3.   
  4. public List<Element> getElementList() {  
  5.     return elementList;  
  6. }  
  7.   
  8.   
  9. public void setElementList(List<Element> elementList) {  
  10.     this.elementList = elementList;  
  11. }  
  12.   
  13.   
  14. /** 
  15.  * @param args 
  16.  */  
  17. public static void main(String[] args) {  
  18.     String[] configLocations = {"E:\\test.xml"};  
  19.     ApplicationContext applicationContext = new FileSystemXmlApplicationContext(configLocations);  
  20.     Test test = (Test)applicationContext.getBean("test");  
  21.     List<Element> elList = test.getElementList();  
  22.     for(Element el : elList){  
  23.         System.out.println(el.getId() + " , " + el.getName() + " , " + el.getUrl());  
  24.     }  
  25. }  

 <?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:aop="http://www.springframework.org/schema/aop"  

    xmlns:tx="http://www.springframework.org/schema/tx"  

  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   

                     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  

                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">   

    <bean id="element0" class="com.beantest.Element">  

        <property name="id" value="1001"/>  

     <property name="name" value="hello"/>  

        <property name="url" value="http://www.baidu.com/"/>  

    </bean>  

    <bean id="element1" class="com.beantest.Element">  

        <property name="id" value="1002"/>  

        <property name="name" value="world"/>  

        <property name="url" value="http://www.google.com/"/>  

    </bean>  

   <bean id="test" class="com.beantest.Test">  

        <property name="elementList">  

            <list>  

                <ref bean="element0" />  

                <ref bean="element1" />  

            </list>  

        </property>  

    </bean>  

</beans>


原文转自:http://blog.csdn.net/xuefeimengli2007/article/details/7948680

0 0
原创粉丝点击