<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- setter property를 이용해 값 주입 -->
<!-- property 태그 이용 -->
<bean name="address" class="di.dto.AddressDTO">
<constructor-arg value="서울시 송파구 가락동"/>
<property name="zipcode">
<value>111-222</value>
</property>
</bean>
<bean name="person"
class="di.dto.PersonDTO"
p:name="이순신"
p:age="30"
p:address-ref="address"/>
</beans>
package di.dto;
public class AddressDTO {
private String zipcode;
private String address;
public AddressDTO() {
}
public AddressDTO(String address) {
super();
this.address = address;
}
public AddressDTO(String zipcode, String address) {
super();
this.zipcode = zipcode;
this.address = address;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "AddressDTO [zipcode=" + zipcode + ", address=" + address + "]";
}
}
package di.dto;
public class PersonDTO {
private String name;
private int age;
private AddressDTO address;
public PersonDTO() {
super();
// TODO Auto-generated constructor stub
}
public PersonDTO(int age) {
this.age = age;
}
public PersonDTO(String name) {
this.name = name;
}
public PersonDTO(AddressDTO address) {
super();
this.address = address;
}
public PersonDTO(String name, int age, AddressDTO address) {
super();
this.name = name;
this.age = age;
this.address = address;
}
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 AddressDTO getAddress() {
return address;
}
public void setAddress(AddressDTO address) {
this.address = address;
}
@Override
public String toString() {
return "PersonDTO [name=" + name + ", age=" + age + ", address="
+ address + "]";
}
}
package di.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import di.dto.AddressDTO;
import di.dto.PersonDTO;
public class TestPerson {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("di/config/person.xml");
AddressDTO ato = (AddressDTO) ctx.getBean("address");
System.out.println(ato);
PersonDTO pto = (PersonDTO) ctx.getBean("person");
System.out.println(pto);
}
}