main
package com.spring;
import java.util.Set;
import org.springframework.context.support.GenericXmlApplicationContext;
public class Main {
public static void main(String[] args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:echo2.xml");
EchoBean echo = (EchoBean) ctx.getBean("echoBean2");
Set<String> keys = echo.map.keySet();
for (String key : keys) {
AnotherBean k = echo.map.get(key);
System.out.println(k.getName()+"\t"+k.getAge());
}
}
}
AnotherBean
package com.spring;
public class AnotherBean {
private String name;
private int age;
@Override
public String toString() {
return "AnotherBean [name=" + name + ", age=" + age + "]";
}
public AnotherBean() {
super();
// TODO Auto-generated constructor stub
}
public AnotherBean(String name, int age) {
super();
this.name = name;
this.age = age;
}
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;
}
}
EchoBean
package com.spring;
import java.util.Map;
public class EchoBean {
Map<String, AnotherBean> map;
@Override
public String toString() {
return "EchoBean [map=" + map + "]";
}
public EchoBean() {
super();
// TODO Auto-generated constructor stub
}
public EchoBean(Map<String, AnotherBean> map) {
super();
this.map = map;
}
public Map<String, AnotherBean> getMap() {
return map;
}
public void setMap(Map<String, AnotherBean> map) {
this.map = map;
}
}
Echo.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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
<bean id="anotherBean1" class="com.spring.AnotherBean">
<property name="name" value="홍길동"/>
<property name="age" value="10"/>
</bean>
<bean id="anotherBean2" class="com.spring.AnotherBean">
<property name="name" value="이순신"/>
<property name="age" value="20"/>
</bean>
<bean id="echoBean" class="com.spring.EchoBean">
<property name="map">
<map>
<entry key="one" value-ref="anotherBean1"/>
<entry key="two">
<ref bean="anotherBean2"/>
</entry>
</map>
</property>
</bean>
<util:map id="map">
<entry key="one" value-ref="anotherBean1"/>
<entry key="two">
<ref bean="anotherBean2"/>
</entry>
</util:map>
<bean id="echoBean2" class="com.spring.EchoBean">
<property name="map" ref="map">
</property>
</bean>
</beans>
'단순 코드 기록 > Spring' 카테고리의 다른 글
Spring_Autowire (0) | 2024.02.08 |
---|---|
Spring_Scope (0) | 2024.02.08 |
Spring_list (0) | 2024.02.08 |
Spring_property (0) | 2024.02.08 |
Spring_constructor-arg (0) | 2024.02.08 |