首页 > 其他 > 详细

Spring的DI(Ioc) - 注入集合类型

时间:2014-02-21 19:50:10      阅读:476      评论:0      收藏:0      [点我收藏+]

1: 首先给service添加集合类型的属性,并提供getter, setter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package cn.gbx.serviceimpl;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
 
import cn.gbx.daoimpl.PersonDao;
import cn.gbx.service.PersonService;
 
public class PersonServiceImpl implements PersonService {
    private Set<String> sets = new HashSet<String>();
    private List<String> list = new ArrayList<String>();
    private Map<String, String> maps = new HashMap<String, String>();
    private Properties properties = new Properties();
     
     
    private PersonDao personDao;
    private String name;
    private Integer id;
     
     
    public void save() {
        System.out.println("id = " + id + "name = " + name);
        personDao.save();
        System.out.println("service :  " + " save 方法");
    }
     
    public PersonDao getPersonDao() {
        return personDao;
    }
    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public Integer getId() {
        return id;
    }
 
    public void setId(Integer id) {
        this.id = id;
    }
 
    public Set<String> getSets() {
        return sets;
    }
 
    public void setSets(Set<String> sets) {
        this.sets = sets;
    }
 
    public List<String> getList() {
        return list;
    }
 
    public void setList(List<String> list) {
        this.list = list;
    }
 
    public Map<String, String> getMaps() {
        return maps;
    }
 
    public void setMaps(Map<String, String> maps) {
        this.maps = maps;
    }
 
    public Properties getProperties() {
        return properties;
    }
 
    public void setProperties(Properties properties) {
        this.properties = properties;
    }
     
}

  

2: 配置xml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?xml version="1.0" encoding="UTF-8"?>
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
            
    <bean id="personDaoImpl" class="cn.gbx.dao.PersonDaoImpl"></bean>
    <bean id="personServiceImpl" class="cn.gbx.serviceimpl.PersonServiceImpl" >
        <property name="personDao" ref="personDaoImpl"></property>
        <property name="name" value="ok-gbx"></property>
        <property name="id" value="22"></property>
         
        <property name="list">
            <list>
                <value>valu1</value>
                <value>valu2</value>
                <value>valu3</value>
            </list>
        </property>
         
        <property name="sets">
            <set>
 
                <value>value-1</value>
                <value>value-2</value>
                <value>value-3</value>
            </set>
        </property>
         
        <property name="maps">
            <map>
                <entry key="key1" value="value1"></entry>
                <entry key="key2" value="value2"></entry>
                <entry key="key3" value="value3"></entry>
            </map>
        </property>
         
        <property name="properties">
            <props>
                <prop key="key1">value1</prop>
                <prop key="key2">value2</prop>
                <prop key="key3">value3</prop>
            </props>
        </property>
    </bean>
</beans>

  

3: 测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class SpringTest {
    @Test
    public void spring1() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
        PersonService ps = (PersonService)ctx.getBean("personServiceImpl");
        ps.save();
        System.out.println("--------List-------------");
        for (String s : ps.getList()) {
            System.out.println(s);
        }
        System.out.println("--------sets-------------");
        for (String s : ps.getSets()) {
            System.out.println(s);
        }
         
        System.out.println("--------maps-------------");
        for (String key : ps.getMaps().keySet()) {
            System.out.println(key + " : " + ps.getMaps().get(key));
        }
         
        System.out.println("--------propertis-------------");
        for (Object key : ps.getProperties().keySet()) {
            System.out.println(key + " : " + ps.getMaps().get(key));
        }
         
    }
     
    @Test
    public void spring2() {
        MyClassPathXmlApplicationContext ctx = new MyClassPathXmlApplicationContext("beans.xml");
        PersonService ps = (PersonService)ctx.getBean("personServiceImpl");
        ps.save();
        System.out.println();
    }
}

  

Spring的DI(Ioc) - 注入集合类型

原文:http://www.cnblogs.com/E-star/p/3558890.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!