<?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-3.0.xsd">
<bean name="director" class="di.dto.DirectorDTO">
<constructor-arg value="d-0001"/>
<constructor-arg value="박찬욱"/>
<constructor-arg value="30"/>
</bean>
<bean name="collection1" class="di.dto.CollectionDTO">
<property name="myList">
<!-- <list value-type="java.lang.Integer"> -->
<list>
<!-- <value type="java.lang.Integer">10</value> -->
<value>10</value>
<value>20</value>
<value>30</value>
<value>40</value>
<value>50</value>
<value>adv</value>
<ref bean="director"/>
</list>
</property>
<property name="names">
<list>
<value>유재석</value>
<value>정준하</value>
<value>정형돈</value>
<value>길</value>
<value>노홍철</value>
</list>
</property>
</bean>
<bean name="collection2" class="di.dto.CollectionDTO">
<property name="mySet">
<set>
<value>10</value>
<value>20</value>
<value>30</value>
<!-- set이라 중복된 값인 30은 두번 들어 가지 않는다. -->
<value>30</value>
<value>abcde</value>
<ref bean="director"/>
</set>
</property>
<property name="myMap">
<map>
<entry>
<key>
<value>감독</value>
</key>
<ref bean="director"/>
</entry>
<entry>
<key>
<value>aaa</value>
</key>
<value>100000</value>
</entry>
<entry key="bbb" value="20000000"/>
<entry key="감독2" value-ref="director"/>
</map>
</property>
<property name="myProps">
<props>
<prop key="aaaa">홍길동</prop>
<prop key="bbbb">이순신</prop>
<prop key="cccc">강감찬</prop>
<prop key="dddd">을지문덕</prop>
</props>
</property>
</bean>
</beans>
package di.dto;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class CollectionDTO {
private List myList;
private String[] names;
private Set mySet;
private Map myMap;
private Properties myProps;
public List getMyList() {
return myList;
}
public void setMyList(List myList) {
this.myList = myList;
}
public String[] getNames() {
return names;
}
public void setNames(String[] names) {
this.names = names;
}
public Set getMySet() {
return mySet;
}
public void setMySet(Set mySet) {
this.mySet = mySet;
}
public Map getMyMap() {
return myMap;
}
public void setMyMap(Map myMap) {
this.myMap = myMap;
}
public Properties getMyProps() {
return myProps;
}
public void setMyProps(Properties myProps) {
this.myProps = myProps;
}
}
package di.dto;
public class DirectorDTO {
private String directorId;
private String name;
private int age;
public DirectorDTO() {
super();
}
public DirectorDTO(String directorId, String name, int age) {
super();
this.directorId = directorId;
this.name = name;
this.age = age;
}
public String getDirectorId() {
return directorId;
}
public void setDirectorId(String directorId) {
this.directorId = directorId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "DirectorDTO [directorId=" + directorId + ", name=" + name
+ ", age=" + age + "]";
}
}
package di.main;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import di.dto.CollectionDTO;
public class TestCollection {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("di/config/collection.xml");
CollectionDTO dto = (CollectionDTO) ctx.getBean("collection1");
List list = dto.getMyList();
for(Object obj: list){
System.out.println(obj.getClass().getName()+" - "+obj);
}
System.out.println("-------------names[]조회-------------");
String [] names = dto.getNames();
for(String name : names){
System.out.println(name);
}
System.out.println("-------mySet조회-------------");
dto = (CollectionDTO) ctx.getBean("collection2");
Set set = dto.getMySet();
for(Object obj:set){
System.out.println(obj);
}
System.out.println("------myMap조회------------");
Map map = dto.getMyMap();
Set keys = map.keySet();
for(Object key:keys){
Object value = map.get(key);
System.out.println(key+" - "+value);
}
System.out.println(map.get("aaa"));
System.out.println(map.get("bbb"));
System.out.println("----------properties조회---------");
Properties props = dto.getMyProps();
Enumeration enu = props.propertyNames();//Enumeration : iterater의 옛버전(collection 조회해 주는 것)
while(enu.hasMoreElements()){
String key = (String) enu.nextElement();
String value = props.getProperty(key);
System.out.println(key+" - "+value);
}
}
}
* 결과
'프로그래밍 > Spring Framework' 카테고리의 다른 글
Bean 객체의 생성 단위 / Factory 메소드를 통한 Bean 주입 (0) | 2012.06.21 |
---|---|
bean scope 테스트(singleton : default, prototype) (0) | 2012.06.21 |
Collection 객체 주입하기 - Spring (0) | 2012.06.20 |
property를 이용한 값 주입(하위태그,속성,p:namespace이용)-spring_di_02[실습] (0) | 2012.06.20 |
property를 이용한 객체 주입-spring_di_02[실습] (0) | 2012.06.20 |