我們可以將值或值列表填充到Spring xml配置文件中定義的Java bean。
我們可以將值或值列表填充到Spring xml配置文件中定義的Java bean。...
為了展示如何使用xml配置文件來填充集合屬性,我們定義了一個(gè)具有四個(gè)集合屬性的Customer對象。
package com.hgci.cnmon; import java.util.ArrayList; import java.util.List; public class Customer { private List<Object> lists = new ArrayList<Object>(); public String toString(){ return lists.toString(); } public List<Object> getLists() { return lists; } public void setLists(List<Object> lists) { this.lists = lists; } }
Person Java Bean
package com.hgci.cnmon; public class Person { private String name; private int age; private String address; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", address=" + address + "]"; } }
以下代碼顯示如何將數(shù)據(jù)填充到j(luò)ava.util.List類型化屬性。
代碼填充三個(gè)值。 第一個(gè)是硬編碼值1.第二個(gè)是一個(gè)bean參考。 我們必須在某處定義PersonBean,以便在此處使用它。 第三個(gè)是bean定義與屬性設(shè)置。
... <property name="lists"> <list> <value>1</value> <ref bean="PersonBean" /> <bean class="com.hgci.cnmon.Person"> <property name="name" value="java2sList" /> <property name="address" value="address" /> <property name="age" value="28" /> </bean> </list> </property> ...
Full Spring的bean配置文件。
<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-2.5.xsd"> <bean id="CustomerBean" class="com.hgci.cnmon.Customer"> <!-- java.util.List --> <property name="lists"> <list> <value>1</value> <ref bean="PersonBean" /> <bean class="com.hgci.cnmon.Person"> <property name="name" value="java2sList" /> <property name="address" value="address" /> <property name="age" value="28" /> </bean> </list> </property> </bean> <bean id="PersonBean" class="com.hgci.cnmon.Person"> <property name="name" value="java2s1" /> <property name="address" value="address 1" /> <property name="age" value="28" /> </bean> </beans>
下面是加載和運(yùn)行配置的代碼。
package com.hgci.cnmon; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml"); Customer cust = (Customer)context.getBean("CustomerBean"); System.out.println(cust); } }
輸出
ListFactoryBean類可以使用ArrayList或創(chuàng)建一個(gè)具體的List集合類LinkedList在Spring的bean配置文件中,然后我們可以注入創(chuàng)建的列表對象到我們的Java bean。
這里是Java bean類。
package com.hgci.cnmon; import java.util.ArrayList; import java.util.List; public class Customer { private List<Object> lists = new ArrayList<Object>(); public String toString(){ return lists.toString(); } public List<Object> getLists() { return lists; } public void setLists(List<Object> lists) { this.lists = lists; } }
這里是Spring的bean配置文件。
<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-2.5.xsd"> //from w w w. ja va 2 s.com <bean id="CustomerBean" class="com.hgci.cnmon.Customer"> <property name="lists"> <bean class="org.springframework.beans.factory.config.ListFactoryBean"> <property name="targetListClass"> <value>java.util.ArrayList</value> </property> <property name="sourceList"> <list> <value>1</value> <value>2</value> <value>3</value> </list> </property> </bean> </property> </bean> </beans>
我們還可以使用util模式和< util:list> 以將數(shù)據(jù)填充到列表中。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd"> <bean id="CustomerBean" class="com.hgci.cnmon.Customer"> <property name="lists"> <util:list list-class="java.util.ArrayList"> <value>1</value> <value>2</value> <value>3</value> </util:list> </property> </bean> </beans>
我們還可以使用util模式和< util:list> 以將數(shù)據(jù)填充到列表中。...
package com.hgci.cnmon; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "SpringBeans.xml"); Customer cust = (Customer) context.getBean("CustomerBean"); System.out.println(cust); } }
更多建議: