The ‘SetFactoryBean’ object provides developer a way to create a concrete Set collection class (HashSet and TreeSet) in Spring’s bean configuration file.

Example

Here’s a SetFactoryBean example, it will instantiate a HashSet at runtime.

package com.mkyong.common;
 
import java.util.Set;
 
public class Customer 
{
	private Set sets;
 
	public Set getSets() {
		return sets;
	}
 
	public void setSets(Set sets) {
		this.sets = sets;
	}
 
	@Override
	public String toString() {
		return "Customer [sets=" + sets + "]" + 
		       " Type=[" + sets.getClass() + "]";
	}
}

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.mkyong.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>

Alternatively, you also can use the util schema and <util:set> to achieve the same thing.

<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.mkyong.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>

Remember to include the util schema, else you will hit the following error

Caused by: org.xml.sax.SAXParseException: 
The prefix "util" for element "util:set" is not bound.

Run it…

package com.mkyong.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(new String[] {"Spring-Customer.xml"});
 
    	Customer cust = (Customer)context.getBean("CustomerBean");
    	System.out.println(cust);
 
    }
}

Ouput

Customer [sets=[3, 2, 1]] Type=[class java.util.HashSet]

You have instantiated and injected a HashSet into Customer’s sets property at runtime.

Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world\'s largest enterprise software company.
Publisher : Oracle Corporation