Spring IoC - 컬렉션객체 주입(Collection Injection) by
Annotation
오라클자바커뮤니티에서 설립한
오엔제이프로그래밍 실무교육센터
(오라클SQL, 튜닝,
힌트,자바프레임워크, 안드로이드, 아이폰, 닷넷 실무전문 강의)
app-context6.xml
<?xml version="1.0"
encoding="UTF-8"?>:namespace prefix = o ns =
"urn:schemas-microsoft-com:office:office" />
<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-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.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 local="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";
}
}
uCollectionAnnotationExam.java
package onj.collection.annotation;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import javax.annotation.Resource;
import
org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.stereotype.Service;
@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;
public Set<Object> getSet() {
return set;
}
public void setSet(Set<Object> set) {
this.set = set;
}
public Properties getProps() {
return props;
}
public void setProps(Properties props) {
this.props = props;
}
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
public void display() {
System.out.println("..............Map..............");
for(Map.Entry<String, Object> entry:map.entrySet()) {
System.out.println("Key: " + entry.getKey() + "-" + entry.getValue());
}
System.out.println("\n...........Properties..........");
for(Map.Entry<Object, Object> entry : props.entrySet()) {
System.out.println("key : " + entry.getKey() + "-" + entry.getValue() );
}
System.out.println("\n.............Set..............");
for(Object obj : set) {
System.out.println("value : " + obj);
}
}
public static void main(String[] args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:app-context6.xml");
ctx.refresh();
CollectionAnnotationExam col =
(CollectionAnnotationExam)ctx.getBean("sample");
col.display();
}
}
[결과]
..............Map..............
Key: someValue-hello... map
Key: someBean-my name is OnJ
...........Properties..........
key : lastName-Lee
key : firstName-Jong Cheol
.............Set..............
value : hello.... set
value : my name is OnJ
댓글 없음:
댓글 쓰기