Spring再接触 集合注入
beans.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean name="userDAO" class="com.bjsxt.dao.impl.UserDAOImpl"> <property name="sets"> <set> <value>1</value> <value>2</value> </set> </property> <property name="lists"> <list> <value>1</value> <value>2</value> <value>3</value> </list> </property> <property name="maps"> <map> <entry key="1" value="1"></entry> <entry key="2" value="2"></entry> <entry key="3" value="3"></entry> <entry key="4" value="4"></entry> </map> </property> </bean> <bean id="userService" class="com.bjsxt.service.UserService"> <!-- <property name="userDAO"> <ref bean="userDAO"/> </property> --> <constructor-arg> <ref bean="userDAO"/> </constructor-arg> </bean> </beans>
userDAOimpl
package com.bjsxt.dao.impl; import java.util.List; import java.util.Map; import java.util.Set; import com.bjsxt.dao.UserDAO; import com.bjsxt.model.User; public class UserDAOImpl implements UserDAO { /* (non-Javadoc) * @see com.bjsxt.dao.UserDAO#save(com.bjsxt.model.User) */ private Set<String> sets; private List<String> lists; private Map<String , String> maps; public Set<String> getSets() { return sets; } public void setSets(Set<String> sets) { this.sets = sets; } public List<String> getLists() { return lists; } public void setLists(List<String> lists) { this.lists = lists; } public Map<String, String> getMaps() { return maps; } public void setMaps(Map<String, String> maps) { this.maps = maps; } public UserDAOImpl() { } public void save(User user) { System.out.println("user saved!"); } @Override public String toString() { return "sets size:" + sets.size() + "| lists size:" + lists.size() + "| maps size:" + maps.size() ; } }
Test
@Test public void testAdd() throws Exception { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); UserDAO u1 = (UserDAO)ctx.getBean("userDAO"); System.out.println(u1); }