We can fill value or a list of values to a Java bean defined in Spring xml configuration file.
The following sections show how to fill data to Set.
In order to show how to use xml configuration file to fill collection properties, we defined a Customer object with four collection properties.
package com.java2s.common; import java.util.HashSet; import java.util.Set; public class Customer { private Set<Object> sets = new HashSet<Object>(); public Set<Object> getSets() { return sets; } public void setSets(Set<Object> sets) { this.sets = sets; } public String toString() { return sets.toString(); } }
Person Java Bean
package com.java2s.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 + "]"; } }
The following code shows how to fill data to a java.util.Set typed property.
The code fills in three values. The first one is hard-coded value 1. The second one is a bean reference. We have to define PersonBean somewhere in order to use it here. The third one is a bean definition with property-setting.
... <property name="sets"> <set> <value>1</value> <ref bean="PersonBean" /> <bean class="com.java2s.common.Person"> <property name="name" value="java2sSet" /> <property name="address" value="address" /> <property name="age" value="28" /> </bean> </set> </property> ...
Full Spring's bean configuration file.
<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.java2s.common.Customer"> <!-- java.util.Set --> <property name="sets"> <set> <value>1</value> <ref bean="PersonBean" /> <bean class="com.java2s.common.Person"> <property name="name" value="java2sSet" /> <property name="address" value="address" /> <property name="age" value="28" /> </bean> </set> </property> </bean> <bean id="PersonBean" class="com.java2s.common.Person"> <property name="name" value="java2s1" /> <property name="address" value="address 1" /> <property name="age" value="28" /> </bean> </beans>
Here is the code to load and run the configuration.
package com.java2s.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); } }
Output
Customer [ sets=[ 1, Person [address=address 1, age=28, name=java2s1], Person [address=address, age=28, name=java2sSet]] ]
SetFactoryBean class can create a concrete Set collection HashSet or TreeSet in Spring's bean configuration file.
The following code shows how to use SetFactoryBean.
Here is the Java bean class.
package com.java2s.common; //from w w w. j av a 2 s. co m import java.util.HashSet; import java.util.Set; public class Customer { private Set<Object> sets = new HashSet<Object>(); public Set<Object> getSets() { return sets; } public void setSets(Set<Object> sets) { this.sets = sets; } public String toString() { return sets.toString(); } }
Here is the Spring's bean configuration file.
<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.java2s.common.Customer"> <property name="sets"> <bean class="org.springframework.beans.factory.config.SetFactoryBean"> <property name="targetSetClass"> <value>java.util.HashSet</value> </property> <property name="sourceSet"> <list> <value>1</value> <value>2</value> <value>3</value> </list> </property> </bean> </property> </bean> </beans>
We also can use util schema and <util:set> to fill data to java.util.Set.
<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.java2s.common.Customer"> <property name="sets"> <util:set set-class="java.util.HashSet"> <value>1</value> <value>2</value> <value>3</value> </util:set> </property> </bean> </beans>
Use the following code to run the application.
package com.java2s.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); } }
The code above generates the following result.