我們可以將值或值列表填充到Spring xml配置文件中定義的Java bean。
以下部分顯示如何向地圖填充數(shù)據(jù)。
為了展示如何使用xml配置文件來填充集合屬性,我們定義了一個具有四個集合屬性的Customer對象。
package com.o2fo.common; import java.util.HashMap; import java.util.Map; public class Customer { private Map<Object, Object> maps = new HashMap<Object, Object>(); public String toString() { return maps.toString(); } public Map<Object, Object> getMaps() { return maps; } public void setMaps(Map<Object, Object> maps) { this.maps = maps; } }
這里是Person bean。
package com.o2fo.common; 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ù)填充到java.util.Set類型化屬性。
代碼填充三個值。 第一個是硬編碼值1.第二個是一個bean參考。 我們必須在某處定義PersonBean,以便在此處使用它。 第三個是bean定義與屬性設置。 當為java.util.Map填充數(shù)據(jù)時,我們必須提供鍵和值對。
... <property name="maps"> <map> <entry key="Key 1" value="1" /> <entry key="Key 2" value-ref="PersonBean" /> <entry key="Key 3"> <bean class="com.o2fo.common.Person"> <property name="name" value="java2sMap" /> <property name="address" value="address" /> <property name="age" value="28" /> </bean> </entry> </map> </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.o2fo.common.Customer"> <!-- java.util.Map --> <property name="maps"> <map> <entry key="Key 1" value="1" /> <entry key="Key 2" value-ref="PersonBean" /> <entry key="Key 3"> <bean class="com.o2fo.common.Person"> <property name="name" value="java2sMap" /> <property name="address" value="address" /> <property name="age" value="28" /> </bean> </entry> </map> </property> </bean> <bean id="PersonBean" class="com.o2fo.common.Person"> <property name="name" value="java2s1" /> <property name="address" value="address 1" /> <property name="age" value="28" /> </bean> </beans>
下面是加載和運行配置的代碼。
package com.o2fo.common; 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); } }
輸出
MapFactoryBean類可以創(chuàng)建Map集合類HashMap或TreeMap在Spring的bean配置文件中。
以下代碼顯示了如何創(chuàng)建HashMap,填充數(shù)據(jù),然后將其注入到bean屬性中。
這里是Java bean類。
package com.o2fo.common; //from w w w . ja va2 s . c om import java.util.HashMap; import java.util.Map; public class Customer { private Map<Object, Object> maps = new HashMap<Object, Object>(); public String toString() { return maps.toString(); } public Map<Object, Object> getMaps() { return maps; } public void setMaps(Map<Object, Object> maps) { this.maps = maps; } }
這里是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.o2fo.common.Customer"> <property name="maps"> <bean class="org.springframework.beans.factory.config.MapFactoryBean"> <property name="targetMapClass"> <value>java.util.HashMap</value> </property> <property name="sourceMap"> <map> <entry key="Key1" value="1" /> <entry key="Key2" value="2" /> <entry key="Key3" value="3" /> </map> </property> </bean> </property> </bean> </beans>
我們還可以使用util模式和< util:map> 以將數(shù)據(jù)填充到java.util.Map。
<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.o2fo.common.Customer"> <property name="maps"> <util:map map-class="java.util.HashMap"> <entry key="Key1" value="1" /> <entry key="Key2" value="2" /> <entry key="Key3" value="3" /> </util:map> </property> </bean> </beans>
這里是運行應用程序的代碼。
package com.o2fo.common; 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); } }
上面的代碼生成以下結(jié)果。
更多建議: