集合注入(很少用)
有的时候我们会使用别人提供我们的类,它里面如果有集合的话,在这个时候我们需要把想注的东西注入进去。
通过<list/>、<set/>、<map/>及<props/>元素可以定义和设置与Java Collection类型对应List、Set、Map及Properties的值。
实现:
实例测试:
UserDaoImpl.java:
然后在beans.xml中配置:
spring会把这些值读取来,设到我们那些属性中去。
测试:(在UserDaoImpl中改写了toString方法,这里直接打印UserDao对象即可)
sets size:2| lists size:3| maps size:4.
有的时候我们会使用别人提供我们的类,它里面如果有集合的话,在这个时候我们需要把想注的东西注入进去。
通过<list/>、<set/>、<map/>及<props/>元素可以定义和设置与Java Collection类型对应List、Set、Map及Properties的值。
实现:
<bean id="moreComplexObject" class="example.ComplexObject"> <!-- results in a setAdminEmails(java.util.Properties) call --> <property name="adminEmails"> <props> <prop key="administrator">administrator@example.org</prop> <prop key="support">support@example.org</prop> <prop key="development">development@example.org</prop> </props> </property> <!-- results in a setSomeList(java.util.List) call --> <property name="someList"> <list> <value>a list element followed by a reference</value> <ref bean="myDataSource" /> </list> </property> <!-- results in a setSomeMap(java.util.Map) call --> <property name="someMap"> <map> <entry> <key> <value>an entry</value> </key> <value>just some string</value> </entry> <entry> <key> <value>a ref</value> </key> <ref bean="myDataSource" /> </entry> </map> </property> <!-- results in a setSomeSet(java.util.Set) call --> <property name="someSet"> <set> <value>just some string</value> <ref bean="myDataSource" /> </set> </property> </bean>
实例测试:
UserDaoImpl.java:
package cn.edu.hpu.dao.Impl; import java.util.List; import java.util.Map; import java.util.Set; import cn.edu.hpu.dao.UserDao; import cn.edu.hpu.model.User; public class UserDaoImpl implements UserDao{ 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 void save(User u) { System.out.println("add success!!"); } @Override public String toString() { return "sets size:"+sets.size()+"| lists size:"+lists.size()+"| maps size:"+maps.size()+"."; } }
然后在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 id="u" class="cn.edu.hpu.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="cn.edu.hpu.service.UserService" > <constructor-arg> <ref bean="u"/> </constructor-arg> </bean> </beans>
spring会把这些值读取来,设到我们那些属性中去。
测试:(在UserDaoImpl中改写了toString方法,这里直接打印UserDao对象即可)
@Test public void testAdd() throws Exception{ BeanFactory ctx=new ClassPathXmlApplicationContext("beans.xml"); UserDao uDao=(UserDao)ctx.getBean("u"); System.out.println(uDao); }结果:
sets size:2| lists size:3| maps size:4.
可以看到已经将各种集合注入。
转载请注明出处:http://blog.csdn.net/acmman