package com.zyiz.netmon; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class Customer { private List<Object> lists; private Set<Object> sets; private Map<Object, Object> maps; private Properties pros; //... }
<property name="lists"> <list> <value>1</value> <ref bean="PersonBean" /> <bean class="com.zyiz.netmon.Person"> <property name="name" value="zyizList" /> <property name="address" value="Hainan" /> <property name="age" value="28" /> </bean> </list> </property>
<property name="sets"> <set> <value>1</value> <ref bean="PersonBean" /> <bean class="com.zyiz.netmon.Person"> <property name="name" value="zyizSet" /> <property name="address" value="Hainan" /> <property name="age" value="28" /> </bean> </set> </property>
<property name="maps"> <map> <entry key="Key 1" value="1" /> <entry key="Key 2" value-ref="PersonBean" /> <entry key="Key 3"> <bean class="com.zyiz.netmon.Person"> <property name="name" value="zyizMap" /> <property name="address" value="Hainan" /> <property name="age" value="28" /> </bean> </entry> </map> </property>
<property name="pros"> <props> <prop key="admin">admin@zyiz.net</prop> <prop key="support">support@zyiz.net</prop> </props> </property>
<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"> <!-- java.util.List --> <property name="lists"> <list> <value>1</value> <ref bean="PersonBean" /> <bean class="com.zyiz.netmon.Person"> <property name="name" value="zyizList" /> <property name="address" value="Hainan Haikou" /> <property name="age" value="28" /> </bean> </list> </property> <!-- java.util.Set --> <property name="sets"> <set> <value>1</value> <ref bean="PersonBean" /> <bean class="com.zyiz.netmon.Person"> <property name="name" value="zyizSet" /> <property name="address" value="Hainan Haikou" /> <property name="age" value="28" /> </bean> </set> </property> <!-- 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.zyiz.netmon.Person"> <property name="name" value="zyizMap" /> <property name="address" value="Hainan Haikou" /> <property name="age" value="28" /> </bean> </entry> </map> </property> <!-- java.util.Properties --> <property name="pros"> <props> <prop key="admin">admin@zyiz.net</prop> <prop key="support">support@zyiz.net</prop> </props> </property> </bean> <bean id="PersonBean" class="com.zyiz.netmon.Person"> <property name="name" value="zyiz1" /> <property name="address" value="Hainan Haikou 1" /> <property name="age" value="28" /> </bean> </beans>
执行程序
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 [lists=[1, com.zyiz.netmon.Person@4e4ee70b, com.zyiz.netmon.Person@1e1867d2], sets=[1, com.zyiz.netmon.Person@4e4ee70b, com.zyiz.netmon.Person@52f644b4], maps={Key 1=1, Key 2=com.zyiz.netmon.Person@4e4ee70b, Key 3=com.zyiz.netmon.Person@54481b6d}, pros={admin=admin@zyiz.net, support=support@zyiz.net}]