集合装配

就是类中的属性是集合,对这种属性进行装配,用的更少。配置例子如下:

我的例子:

  UserDAOImpl

 1 package com.bjsxt.dao.impl;
 2 
 3 import java.util.List;
 4 import java.util.Map;
 5 import java.util.Set;
 6 
 7 import com.bjsxt.dao.UserDAO;
 8 import com.bjsxt.model.User;
 9 
10 public class UserDAOImpl implements UserDAO{
11 
12     private Set<String> sets;
13     
14     private List<String> lists;
15     
16     private Map<String,String> maps;
17     
18     public void save(User u) {
19         System.out.println("a user saved!");
20     }
21 
22     public Set<String> getSets() {
23         return sets;
24     }
25 
26     public void setSets(Set<String> sets) {
27         this.sets = sets;
28     }
29 
30     public List<String> getLists() {
31         return lists;
32     }
33 
34     public void setLists(List<String> lists) {
35         this.lists = lists;
36     }
37 
38     public Map<String, String> getMaps() {
39         return maps;
40     }
41 
42     public void setMaps(Map<String, String> maps) {
43         this.maps = maps;
44     }
45     
46     public String toString(){
47         return "sets size:" + sets.size() + "| lists size:" + lists.size() + "| maps size:" + maps.size();
48     }
49 
50 }
View Code

  beans.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans
 5            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 6 
 7   <bean name="u" class="com.bjsxt.dao.impl.UserDAOImpl">
 8       <property name="sets">
 9           <set>
10               <value>1</value>
11               <value>2</value>
12           </set>
13       </property>
14       <property name="lists">
15           <list>
16               <value>1</value>
17               <value>2</value>
18               <value>3</value>
19           </list>
20       </property>
21       <property name="maps">
22           <map>
23               <entry key="1" value="1"></entry>
24               <entry key="2" value="2"></entry>
25               <entry key="3" value="3"></entry>
26               <entry key="4" value="4"></entry>
27           </map>
28       </property>
29   </bean>
30 
31   <bean id="userService" class="com.bjsxt.service.UserService" scope="prototype">
32       <!-- 
33       <property name="userDAO">
34           <ref bean="u"/>
35       </property>
36        -->
37        <constructor-arg>
38            <ref bean="u"/>
39        </constructor-arg>
40   </bean>
41 </beans>

  UserServiceTest

 1 package com.bjsxt.service;
 2 
 3 import org.junit.Test;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 import com.bjsxt.dao.UserDAO;
 8 import com.bjsxt.dao.impl.UserDAOImpl;
 9 
10 public class UserServiceTest {
11 
12     @Test
13     public void testAddUser() throws Exception {
14         ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");//初始化ApplicationContext对象,加载配置文件
15         
16         UserDAO u1 = (UserDAOImpl)ctx.getBean("u");
17         System.out.println(u1);
18     }
19 
20 }

代码链接: http://pan.baidu.com/s/1o78eG22 密码: xej7

jar包链接: http://pan.baidu.com/s/1qYRTGEo 密码: fejx

原文地址:https://www.cnblogs.com/ShawnYang/p/6879813.html