EchoBeanTest
package com.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class EchoBeanTest {
public static void main(String[] args) {
ApplicationContext ctx = new GenericXmlApplicationContext("classpath:com/spring/echo2.xml");
EchoBean echo = (EchoBean)ctx.getBean("echoBean");
System.out.println(echo.getValueList());
for (AnotherBean string : echo.getValueList()) {
System.out.println(string.getName());
}
}
}
AnotherBean
package com.spring;
public class AnotherBean {
String name;
@Override
public String toString() {
return "AnotherBean [name=" + name + "]";
}
public AnotherBean() {
super();
// TODO Auto-generated constructor stub
}
public AnotherBean(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
EchoBean
package com.spring;
import java.util.List;
public class EchoBean {
List<AnotherBean> valueList; //set함수를 이용한 주입
AnotherBean anotherBean; //생성자를 이용한 주입
public EchoBean() {
super();
}
public EchoBean(AnotherBean anotherBean) {
super();
this.anotherBean = anotherBean;
}
public String sayEcho() {
return "hello";
}
public List<AnotherBean> getValueList() {
return valueList;
}
public void setValueList(List<AnotherBean> valueList) {
this.valueList = valueList;
}
public AnotherBean getAnotherBean() {
return anotherBean;
}
public void setAnotherBean(AnotherBean anotherBean) {
this.anotherBean = anotherBean;
}
}
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
constructor-arg: 생성자 사용
property: Setter사용
-->
<bean id="anotherBean1" class="com.spring.AnotherBean">
<constructor-arg name="name" value="test1"/>
</bean>
<bean id="anotherBean2" class="com.spring.AnotherBean">
<constructor-arg name="name" value="test2"/>
</bean>
<bean id="anotherBean3" class="com.spring.AnotherBean">
<constructor-arg name="name" value="test3"/>
</bean>
<bean id="echoBean" class="com.spring.EchoBean">
<property name="valueList">
<list>
<ref bean="anotherBean1"/>
<ref bean="anotherBean2"/>
<ref bean="anotherBean3"/>
</list>
</property>
</bean>
</beans>
echo2.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">
<constructor-arg name="name" value="test1"/>
</bean>
<bean id="anotherBean2" class="com.spring.AnotherBean">
<constructor-arg name="name" value="test2"/>
</bean>
<bean id="anotherBean3" class="com.spring.AnotherBean">
<constructor-arg name="name" value="test3"/>
</bean>
<util:list id="list">
<ref bean="anotherBean1"/>
<ref bean="anotherBean2"/>
<ref bean="anotherBean3"/>
</util:list>
<bean id="echoBean" class="com.spring.EchoBean">
<property name="valueList" ref="list"/>
</bean>
</beans>
'단순 코드 기록 > Spring' 카테고리의 다른 글
Spring_Scope (0) | 2024.02.08 |
---|---|
Spring_map (0) | 2024.02.08 |
Spring_property (0) | 2024.02.08 |
Spring_constructor-arg (0) | 2024.02.08 |
Spring_Porn.xml (0) | 2024.02.08 |