personBeanConstructContext.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.3.xsd">
<bean id="p1" class="kr.or.iei.person.model.vo.Person">
<constructor-arg>
<value>홍길동</value>
</constructor-arg>
<constructor-arg>
<value>20</value>
</constructor-arg>
<constructor-arg>
<value>경기도 부천</value>
</constructor-arg>
</bean>
<bean id="pm" class="kr.or.iei.person.model.vo.PersonManager">
<constructor-arg ref="p1"></constructor-arg>
</bean>
</beans>
Dependency3Servlet.java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
AbstractApplicationContext context = new GenericXmlApplicationContext("/personBeanConstructContext.xml");
PersonManager pm = context.getBean("pm", PersonManager.class);
Person ps = pm.getPs();
System.out.println("이름 : " + ps.getName());
System.out.println("나이 : " + ps.getAge());
System.out.println("주소 : " + ps.getAddr());
}
PersonManager.java
public class PersonManager {
private Person ps;
public PersonManager(Person ps) {
super();
this.ps = ps;
}
public PersonManager() {
super();
}
public Person getPs() {
return ps;
}
public void setPs(Person ps) {
this.ps = ps;
}
}
Person.java
public class Person {
private String name;
private int age;
private String addr;
public Person() {
super();
}
public Person(String name, int age, String addr) {
super();
this.name = name;
this.age = age;
this.addr = addr;
}
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;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
}