package com.zyiz.netmon; import java.util.Set; public class Customer { private Set sets; //... }
<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.zyiz.netmon.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>one</value> <value>2</value> <value>three</value> </list> </property> </bean> </property> </bean> </beans>
<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.zyiz.netmon.Customer"> <property name="sets"> <util:set set-class="java.util.HashSet"> <value>one</value> <value>2</value> <value>three</value> </util:set> </property> </bean> </beans>
Caused by: org.xml.sax.SAXParseException: The prefix "util" for element "util:set" is not bound.
执行结果
package com.zyiz.netmon; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Customer cust = (Customer)context.getBean("CustomerBean"); System.out.println(cust); } }
输出结果:
Customer [sets=[2, one, three]] Type=[class java.util.HashSet]