1. List 타입 프로퍼티 전달받기
1) ListCollection
package com.model.di.exam06;
import java.util.List;
public class ListCollection {
private List<String> userName;
private List<String> getUserName(){
return userName;
}
public void setUserName(List<String> userName) {
this.userName = userName;
}
public void display() {
System.out.println("********** list 목록 ********** ");
for(String name : userName)
System.out.println(name);
}
}
2) 실행 클래스
package com.model.di.exam06;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.model.di.exam05.SystemMonitor;
public class Main {
public static void main(String[] args) {
AbstractApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
context.registerShutdownHook();
ListCollection list = (ListCollection)context.getBean("list");
list.display();
context.close();
}
}
3) 설정 파일
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- com.model.di.exam06 -->
<!-- List 타입 프로퍼티 설정 -->
<bean id="list" class="com.model.di.exam06.ListCollection">
<property name="userName">
<list value-type="java.lang.String">
<value>홍길동</value>
<value>이순신</value>
<value>강감찬</value>
</list>
</property>
</bean>
</beans>
2. Map 타입 프로퍼티 전달받기
1) MapCollection
package com.model.di.exam07;
import java.util.Map;
public class MapCollection {
private Map<String, Object> map;
public void setMap(Map<String, Object> map){
this.map = map;
}
public void display() {
System.out.println("********* map 목록 *********");
System.out.println(map);
}
}
2) 실행 클래스
package com.model.di.exam07;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.model.di.exam06.ListCollection;
public class Main {
public static void main(String[] args) {
AbstractApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
context.registerShutdownHook();
MapCollection map = (MapCollection)context.getBean("map");
map.display();
context.close();
}
}
3) 설정 파일
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- com.model.di.exam07 -->
<!-- Map 타입 프로퍼티 설정 -->
<bean id="map" class="com.model.di.exam07.MapCollection">
<property name="map">
<map key-type="java.lang.String" value-type="java.lang.Object">
<entry key="홍길동" value="20"></entry>
<entry key="이순신" value="30"></entry>
<entry key="강감찬" value="40"></entry>
</map>
</property>
</bean>
' Spring Framework' 카테고리의 다른 글
Bean 객체의 타입을 이용하여 의존 객체를 주입 (autowire="byType") (0) | 2016.03.28 |
---|---|
프로퍼티 이름을 이용한 의존 관계 자동 설정 (autowire="byName") (0) | 2016.03.28 |
생성자를 이용하여 자료를 전달 (constructor-arg) (0) | 2016.03.28 |
스키마 p(namespaces)를 이용한 값 전달 (0) | 2016.03.28 |
설정파일에서 객체를 전달하는 방법: 생성자 방식, setter 메서드 방식 (0) | 2016.03.25 |