Spring Collection (List, Map, Properties, Set) 주입

스프링에서는 개별 <value> 뿐만 아니라, <map>, <set>, <list>, <props>태그를 지원하여 대상 class의 attribute가 collection객체인 경우도 bean생성을 지원하도록 도와준다. 다만, xml을 이용한 스프링 구조에서는 별다를게 없지만, annotation 방식에서는 두가지 사항을 유의해야 한다. 


1. Collection객체는 @Auttowired 사용불가/ 대신 @Resource(name="")를 사용해야 함

@Autowired는 타입을 기반으로 주입을 한다. List<Oracle>과 같은 attribute에 @Autowired를 지정했고, Oracle타입으로 미리생성된 bean들이 있다면 내가 List<Oracle>에 넣으려고 만든 Oracle이 아니라 미리 생성된 bean들이 List에 할당될지도 모른다. 즉 어떤 객체가 할당되게 될 지 파악하기 힘들다.


@Resource는 (name="")을 통해서 id 기반으로 주입을 하기 때문에 @Auttowired처럼 타입만 보고 지정하는 경우의 문제를 해결할 수 있다. 

2. @Resource annotation을 사용하기 위해서는 JSR-250에 해당하는 jar파일 필요

pom.xml에 아래 dependencyt 추가


<dependency>

   <groupId>javax.annotation</groupId>

   <artifactId>jsr250-api</artifactId>

   <version>1.0</version>

</dependency> 

Collection주입 (xml)

[DummyVo Class]

 

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
package com.ljs.vo;
 
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
 
public class DummyVo {
    // private member
    private Map<String, Object> map;
    private Properties props;
    private Set<Object> set;
    private List list;
    
    // getters and setters
    public Map<String, Object> getMap() {
        return map;
    }
    public void setMap(Map<String, Object> map) {
        this.map = map;
    }
    public Properties getProps() {
        return props;
    }
    public void setProps(Properties props) {
        this.props = props;
    }
    public Set<Object> getSet() {
        return set;
    }
    public void setSet(Set<Object> set) {
        this.set = set;
    }
    public List<?> getList() {
        return list;
    }
    public void setList(List<?> list) {
        this.list = list;
    }
    
    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("***Map***\n");
        
        // map print
        for(Map.Entry<String, Object> entry:map.entrySet() ) {
            sb.append("KEY : "+entry.getKey()+", VALUE : "+entry.getValue()+"\n");
        }
        
        // properties print
        sb.append("***Properties***\n");
        for(Map.Entry<Object, Object> entry:props.entrySet()) {
            sb.append("KEY : "+entry.getKey()+", VALUE : "+entry.getValue()+"\n");
        }
        
        sb.append("***Set***\n");
        for(Object obj:set) {
            sb.append(obj+"|");
        }
        
        sb.append("\n***List***\n");
        for(Object obj:list) {
            sb.append(obj+"|");
        }
        
        return sb.toString();
    }
}
cs


[app-context.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
47
48
49
50
51
52
<?xml version="1.0" encoding="UTF-8"?>
        
    <bean id="stringBean" class="java.lang.String">
        <constructor-arg><value>자바빈주입</value></constructor-arg>
    </bean>
    
    <bean id="voBean" class = "com.ljs.vo.DummyVo">
        <property name="map">
            <map>
                <entry key="soemValue">
                    <value>안녕</value>
                </entry>
                <entry key="someBean">
                    <ref bean="stringBean"/>
                </entry>
            </map>
        </property>
        
        <property name="props">
            <props>
                <prop key="firstName">이</prop>
                <prop key="secondName">지수</prop>
            </props>
        </property>
        
        <property name="set">
            <set>
                <value>Hello world</value>
                <ref bean="stringBean"/>
            </set>
        </property>
        
        <property name="list">
            <list>
                <value>Hello world</value>
                <ref bean="stringBean"/>
            </list>
        </property>
    </bean>
</beans>
cs


Collection주입(Annotation)

[DummyVo Class]

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
package com.ljs.vo;
 
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
 
import javax.annotation.Resource;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
@Service("voBean")
public class DummyVo {
    // private member
    
    //@Resource(name="map")
    @Autowired
    private Map<String, Object> map;
    @Resource(name="props")
    private Properties props;
    @Resource(name="set")
    private Set<Object> set;
    @Resource(name="list")
    private List<?> list;
    
    // getters and setters
    public Map<String, Object> getMap() {
        return map;
    }
    public void setMap(Map<String, Object> map) {
        this.map = map;
    }
    public Properties getProps() {
        return props;
    }
    public void setProps(Properties props) {
        this.props = props;
    }
    public Set<Object> getSet() {
        return set;
    }
    public void setSet(Set<Object> set) {
        this.set = set;
    }
    public List<?> getList() {
        return list;
    }
    public void setList(List<?> list) {
        this.list = list;
    }
    
    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("***Map***\n");
        
        // map print
        for(Map.Entry<String, Object> entry:map.entrySet() ) {
            sb.append("KEY : "+entry.getKey()+", VALUE : "+entry.getValue()+"\n");
        }
        
        // properties print
        sb.append("***Properties***\n");
        for(Map.Entry<Object, Object> entry:props.entrySet()) {
            sb.append("KEY : "+entry.getKey()+", VALUE : "+entry.getValue()+"\n");
        }
        
        sb.append("***Set***\n");
        for(Object obj:set) {
            sb.append(obj+"|");
        }
        
        sb.append("\n***List***\n");
        for(Object obj:list) {
            sb.append(obj+"|");
        }
        
        return sb.toString();
    }
}
cs



[app-context.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
47
48
<?xml version="1.0" encoding="UTF-8"?>
        
    <!--스프링이 코드기반에서 의존성 요구조건을 스캔하게끔 세팅-->    
    <context:annotation-config/>
    <!--스프링이 코드에서 지정된 패키지(및 하위)아래에 있는 주입 가능한 빈을 모두 스캔하도록 세팅-->
    <context:component-scan base-package="com.ljs"/>
 
    <bean id="stringBean" class="java.lang.String">
        <constructor-arg><value>자바빈주입</value></constructor-arg>
    </bean>
    
    <util:map id="map" map-class="java.util.HashMap">
        <entry key="soemValue">
            <value>안녕</value>
        </entry>
        <entry key="someBean">
            <ref bean="stringBean"/>
        </entry>
    </util:map>
    
    <util:properties id="props">
        <prop key="firstName">이</prop>
        <prop key="secondName">지수</prop>
    </util:properties>
    
    <util:set id="set">
        <value>Hello world</value>
        <ref bean="stringBean"/>
    </util:set>
 
    
    <util:list id="list">
        <value>Hello world</value>
        <ref bean="stringBean"/>
    </util:list>
</beans>
cs


다운로드


이 글을 공유하기

댓글

Email by JB FACTORY