컬렉션 주입(Collection Injection), XML 방식
n 빈에서 개별 빈 이나 값이 아닌 객체의 컬렉션에 접근해야 되는 경우가 있는데 <list>, <map>, <set>, <prop>를 사용한다. 자바에서 Properties 클래스는 property 값으로 String을 지원하므로 String만 넘길 수 있다.
[app-context5.xml]
<?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-4.2.xsd">
<bean id="onj" name="onjName" class="onj.injection.collection.Onj"/>
<bean id="sample" class="onj.injection.collection.CollectionExam">
<property name="map">
<map>
<entry key="someValue">
<value>hello map...</value>
</entry>
<entry key="someBean">
<ref bean="onj"/>
</entry>
</map>
</property>
<property name="set">
<set>
<value>hello list...</value>
<ref bean="onj"/>
</set>
</property>
<property name="props">
<props>
<prop key="firstName">Jong Cheol</prop>
<prop key="lastName">Lee</prop>
</props>
</property>
</bean>
</beans>
[CollectionExam.java]
public void display() {
for(Map.Entry<String, Object> entry:map.entrySet()) {
System.out.println("Key: " + entry.getKey() + "-" + entry.getValue());
}
for(Map.Entry<Object, Object> entry : props.entrySet()) {
System.out.println("key : " + entry.getKey() + "-" + entry.getValue() );
}
for(Object obj : set) {
System.out.println("value : " + obj);
}
}
public static void main(String[] args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:app-context5.xml");
ctx.refresh();
CollectionExam col = (CollectionExam)ctx.getBean("sample“)
col.display();
}
컬렉션 주입(Collection Injection), 어노테이션 방식
[app-context6.xml]
<?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:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<context:annotation-config/>
<context:component-scan base-package="onj.collection.annotation" />
<bean id="onj" name="onjName" class="onj.collection.annotation.Onj"/>
<util:map id="map" map-class="java.util.HashMap">
<entry key="someValue">
<value>hello... map</value>
</entry>
<entry key="someBean">
<ref ben="onj"/>
</entry>
</util:map>
<util:properties id="props">
<prop key="firstName">Jong Cheol</prop>
<prop key="lastName">Lee</prop>
</util:properties>
<util:set id="set">
<value>hello.... set</value>
<ref bean="onj"/>
</util:set>
</beans>
[Onj.java]
package onj.collection.annotation;
import org.springframework.stereotype.Service;
@Service("onj")
public class Onj {
public String toString() {
return "my name is OnJ";
}
}
[CollectionAnnotationExam.java]
…
…
@Service("sample")
public class CollectionAnnotationExam {
@Resource(name="map")
private Map<String, Object> map ;
@Resource(name="props")
private Properties props;
@Resource(name="set")
private Set<Object> set;
…
…
…
댓글 없음:
댓글 쓰기