Spring中的集合的注入方式
jopen
9年前
上代码 bean.xml的配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <bean id="collectionBean" class="org.heinrich.CollectionBean"> <property name="sets"> <set> <value>Heinrich</value> <value>Reading Thinking Java</value> </set> </property> <property name="users"> <list> <ref bean="user1"/> <ref bean="user2"/> </list> </property> <property name="maps"> <map> <entry key="BookName"> <value>Thinking Java</value> </entry> <entry key="PhoneName"> <value>Apple 6 plus</value> </entry> </map> </property> <property name="properties"> <props> <prop key="1">MySQL</prop> <prop key="2">Oracle</prop> <prop key="3">SQLSERVER</prop> </props> </property> </bean> <bean id="user1" class="org.heinrich.User"> <property name="name"> <value>Heinrich</value> </property> <property name="age"> <value>21</value> </property> </bean> <bean id="user2" class="org.heinrich.User"> <property name="name"> <value>Zhangyanmei</value> </property> <property name="age"> <value>22</value> </property> </bean> </beans>
package org.heinrich; public class User { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
上面是实体类User,下面是需要注入的实体类
package org.heinrich; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class CollectionBean { private List<User> users; private Map<String,String> maps; private Set<String> sets; private Properties properties; public List<User> getUsers() { return users; } public void setUsers(List<User> users) { this.users = users; } public Map<String, String> getMaps() { return maps; } public void setMaps(Map<String, String> maps) { this.maps = maps; } public Set<String> getSets() { return sets; } public void setSets(Set<String> sets) { this.sets = sets; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } }
测试类
package org.heinrich; import org.springframework.context.support.ClassPathXmlApplicationContext; public class BeanTest { public static void main(String[] args) { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml"); User user1 = (User)ctx.getBean("user1"); User user2 = (User)ctx.getBean("user1"); System.out.println(user1); System.out.println(user2); CollectionBean collectionBean = (CollectionBean)ctx.getBean("collectionBean"); System.out.println(collectionBean); System.out.println(collectionBean.getMaps()); System.out.println(collectionBean.getSets()); System.out.println(collectionBean.getUsers()); System.out.println(collectionBean.getProperties()); } }
测试结果
org.heinrich.User@f8dd88b org.heinrich.User@f8dd88b org.heinrich.CollectionBean@298395a7 {BookName=Thinking Java, PhoneName=Apple 6 plus} [Heinrich, Reading Thinking Java] [org.heinrich.User@f8dd88b, org.heinrich.User@7dd61c3b] {3=SQLSERVER, 2=Oracle, 1=MySQL}