hyeonga_code
Spring_Spring DI_컬렉션 타입_ 컬렉션 객체 설정 본문
- 컬렉션 객체 설정
- 태그 목록
- : 다른 스프링 빈 객체를 값으로 사용합니다.
- : 임의의 빈 객체를 생성하여 값으로 사용합니다.
- : 래퍼 타입이나 String을 값으로 사용합니다.
- <list>, <map>, <props>, <set> : 컬렉션 객체를 값으로 사용합니다.
- : null 레퍼런스를 값으로 사용합니다.
- <list>
- 리스트 타입, 배열에 값 목록을 전달할 때 사용합니다.
- <map>
- Map 타입에 <키, 값> 목록을 전달할 때 사용합니다.
- <set>
- set 타입에 값 목록을 전달할 때 사용합니다.
- <props>
- properties 타입에 <프로퍼티 이름, 프로퍼티 값> 목록을 전달할 때 사용합니다.
- <List> 타입과 배열
- sts.spring.list 패키지 생성 > CollectionBean.java 클래스를 생성합니다.
=====
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package sts.spring.list;
import java.util.List;
public class CollectionBean {
private List<String> addressList;
public List<String> getAddressList(){
return addressList;
}
public void setAddressList(List<String> addressList) {
this.addressList = addressList;
}
}
|
- applicationContext.xml 파일에 bean 객체를 등록합니다.
=====
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- sts.spring.list -->
<bean id="collectionBean" class="sts.spring.list.CollectionBean">
<property name="addressList">
<list>
<value>Seoul</value>
<value>Busan</value>
</list>
</property>
</bean>
</beans>
|
- sts.spring.list > CollectionBeanClient.java 클래스를 생성합니다.
=====
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
|
package sts.spring.list;
import java.util.List;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class CollectionBeanClient {
public static void main(String[] args) {
AbstractApplicationContext fac
= new GenericXmlApplicationContext("applicationContext.xml"); // applicationContext.xml에서 설정한 bean id
CollectionBean bean = (CollectionBean) fac.getBean("collectionBean");
List<String> addressList = bean.getAddressList();
for(String addr : addressList) {
System.out.println(addr.toString());
}
// 컨테이너 종료
fac.close();
/*
Seoul
Busan
*/
}
}
|
- <map> 타입
- sts.spring.map 패키지 생성 > CollectionBean.java 클래스 생성
=====
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package sts.spring.map;
import java.util.Map;
public class CollectionBean {
private Map<String, String> addressList;
public Map<String, String> getAddressList(){
return addressList;
}
public void setAddressList(Map<String, String> addressList) {
this.addressList = addressList;
}
}
|
- applicaionContext.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
|
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- sts.spring.map -->
<!-- 다른 스프링 빈 객체를 키로 사용한다? <ref bean="restHandler"/> -->
<!-- 한 개의 entry 태그는 map에 저장되는 하나의 키,값을 표현합니다. -->
<bean name="collectionBean" class="sts.spring.map.CollectionBean">
<property name="addressList">
<map>
<entry>
<key><value>Person 1</value></key>
<value>Seoul</value>
</entry>
<entry>
<key><value>Person 2</value></key>
<value>Busan</value>
</entry>
</map>
</property>
</bean>
</beans>
|
- CollectionBeanClient.java 클래스 생성
=====
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
|
package sts.spring.map;
import java.util.Map;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class CollectionBeanClient {
public static void main(String[] args) {
AbstractApplicationContext fac
= new GenericXmlApplicationContext("applicationContext.xml"); // applicationContext.xml에서 설정한 bean id
CollectionBean bean = (CollectionBean) fac.getBean("collectionBean");
Map<String, String> addressList = bean.getAddressList();
for(String key : addressList.keySet()) {
System.out.println(String.format("%s : %s ", key, addressList.get(key)));
}
// 컨테이너 종료
fac.close();
/*
Person 1 : Seoul
Person 2 : Busan
*/
}
}
|
- <properties>
- sts.spring.property 패키지 생성 > CollectionBean.java 클래스 생성
=====
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package sts.spring.property;
import java.util.Properties;
public class CollectionBean {
private Properties addressList;
public Properties getAddressList() {
return addressList;
}
public void setAddressList(Properties addressList) {
this.addressList = addressList;
}
}
|
- applicationContext.xml 파일 수정
=====
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- sts.spring.property -->
<bean id="collectionBean" class="sts.spring.property.CollectionBean">
<property name="addressList">
<props>
<prop key="person3">Seoul</prop>
<prop key="person4">Busan</prop>
</props>
</property>
</bean>
</beans>
|
- sts.spring.property > CollectionBeanClient.java 클래스 생성
=====
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
|
package sts.spring.property;
import java.util.Properties;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class CollectionBeanClient {
public static void main(String[] args) {
AbstractApplicationContext fac = new GenericXmlApplicationContext("applicationContext.xml");
// applicationContext.xml에서 설정한 bean id
CollectionBean bean = (CollectionBean) fac.getBean("collectionBean");
Properties addressList = bean.getAddressList();
for(String key : addressList.stringPropertyNames()) {
System.out.println(String.format("%s : %s ", key, addressList.get(key)));
}
// 컨테이너 종료
fac.close();
/*
Person 3 : Seoul
Person 4 : Busan
*/
}
}
|
- <set>
- sts.spring.set 패키지 생성 > CollectionBean.java 클래스 생성
=====
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package sts.spring.set;
import java.util.Set;
public class CollectionBean {
private Set<String> addressList;
public Set<String> getAddressList() {
return addressList;
}
public void setAddressList(Set<String> addressList) {
this.addressList = addressList;
}
}
|
- applicationContext.xml 파일 수정
=====
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- sts.spring.set -->
<bean id="collectionBean" class="sts.spring.set.CollectionBean">
<property name="addressList">
<set value-type="java.lang.String">
<value>Seoul</value>
<value>Busan</value>
<value>Jeju</value>
</set>
</property>
</bean>
</beans>
|
- sts.spring.set > CollectionBeanClient.java 클래스 생성
=====
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
|
package sts.spring.set;
import java.util.Set;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class CollectionBeanClient {
public static void main(String[] args) {
AbstractApplicationContext fac = new GenericXmlApplicationContext("applicationContext.xml");
// applicationContext.xml에서 설정한 bean id
CollectionBean bean = (CollectionBean) fac.getBean("collectionBean");
Set<String> addressList = bean.getAddressList();
for(String key : addressList) {
System.out.println(key);
}
// 컨테이너 종료
fac.close();
/*
Seoul
Busan
Jeju
*/
}
}
|
'Spring' 카테고리의 다른 글
Spring_AOP 심화 실습_ 1. 기본 설정 (0) | 2024.01.08 |
---|---|
Spring_의존성 주입_어노테이션 기반 설정 (0) | 2023.12.17 |
Spring_의존성 주입_p, c 네임스페이스 사용하기, 룩업 메소드 인젝션 방식 (0) | 2023.12.16 |
Spring_의존성 주입_Setter 인젝션 사용하기 (0) | 2023.12.16 |
Spring_의존성 주입_의존 관계의 변경 (0) | 2023.12.16 |