접기
package di.dto;
public class AddressDTO { private String zipcode; private String address; public AddressDTO(String zipcode, String address) { super(); this.zipcode = zipcode; this.address = address; } public AddressDTO() { } @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(int age) { this.age = age; } public PersonDTO(String name) { this.name = name; } public PersonDTO(String name, int age, AddressDTO address) { super(); this.name = name; this.age = age; this.address = address; } @Override public String toString() { return "PersonDTO [name=" + name + ", age=" + age + ", address=" + address + "]"; } }
접기
접기
<?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-3.0.xsd "> <!-- AddressDTO --> <!-- no-arg 생성자 호출 하여 객체 생성 --> <bean name="address1" class="di.dto.AddressDTO"/> <bean name="address2" class="di.dto.AddressDTO"> <constructor-arg> <value>111-222</value> </constructor-arg> <constructor-arg> <value>서울시 송파구 가락동</value> </constructor-arg> </bean> </beans>
접기
접기
package di.main;
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
import di.dto.AddressDTO;
public class TestPerson { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("di/dto/config/person.xml"); AddressDTO ato1 = (AddressDTO) ctx.getBean("address1"); System.out.println(ato1); AddressDTO ato2 = (AddressDTO) ctx.getBean("address2"); System.out.println(ato2); } }
접기