TestStudent
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.spring.Person;
public class TestStudent {
public static void main(String[] args) {
ApplicationContext ctx = new GenericXmlApplicationContext("classpath:Echo.xml");
Person p1 = (Person)ctx.getBean("person");
System.out.println(p1);
System.out.println(p1.getName());
System.out.println(p1.getAge());
System.out.println(p1.getCat());
System.out.println(p1.getCat().getCatName());
System.out.println(p1.getCat().getCatAge());
}
}
Cat(DTO)
package com.spring;
public class Cat {
String catName;
int catAge;
@Override
public String toString() {
return "Cat [catName=" + catName + ", catAge=" + catAge + "]";
}
public Cat() {
super();
// TODO Auto-generated constructor stub
}
public Cat(String catName, int catAge) {
super();
this.catName = catName;
this.catAge = catAge;
}
public String getCatName() {
return catName;
}
public void setCatName(String catName) {
this.catName = catName;
}
public int getCatAge() {
return catAge;
}
public void setCatAge(int catAge) {
this.catAge = catAge;
}
}
Dog(DTO)
package com.spring;
public class Dog {
String dogName;
int dogAge;
@Override
public String toString() {
return "dog [dogName=" + dogName + ", dogAge=" + dogAge + "]";
}
public Dog() {
super();
// TODO Auto-generated constructor stub
}
public Dog(String dogName, int dogAge) {
super();
this.dogName = dogName;
this.dogAge = dogAge;
}
public String getDogName() {
return dogName;
}
public void setDogName(String dogName) {
this.dogName = dogName;
}
public int getDogAge() {
return dogAge;
}
public void setDogAge(int dogAge) {
this.dogAge = dogAge;
}
}
Person(DTO) - Cat(DTO), Dog(DTO) 참조
package com.spring;
public class Person {
private String name;
private int age;
private Cat cat;
private Dog dog;
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", cat=" + cat + ", dog=" + dog + "]";
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String name, int age, Cat cat, Dog dog) {
super();
this.name = name;
this.age = age;
this.cat = cat;
this.dog = dog;
}
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 Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
}
echo.xml1(property 사용)
<?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">
<bean id="cat1" class="com.spring.Cat">
<property name="catName" value="야옹이"></property>
<property name="catAge" value="10"></property>
</bean>
<bean id="dog1" class="com.spring.Dog">
<property name="dogName" value="멍멍이"></property>
<property name="dogAge" value="15"></property>
</bean>
<bean id="xxx" class="com.spring.Person">
<property name="name" value="홍길동"></property>
<property name="age" value="20"></property>
<property name="cat" ref="cat1"></property>
<property name="dog" ref="dog1"></property>
</bean>
</beans>
echo.xml2(p속성 사용)
<?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.xsd">
<bean id="cat1" class="com.spring.Cat" p:catName="야옹이" p:catAge="10">
</bean>
<bean class="com.spring.Person" id="person" p:name="홍길동" p:age="10" p:cat-ref="cat1">
<!-- Web에서는 id를 부여 안해주면 자동으로 소문자로 부여해줌 -->
</bean>
</beans>
Property: Setter사용
'단순 코드 기록 > Spring' 카테고리의 다른 글
Spring_map (0) | 2024.02.08 |
---|---|
Spring_list (0) | 2024.02.08 |
Spring_constructor-arg (0) | 2024.02.08 |
Spring_Porn.xml (0) | 2024.02.08 |
Spring_dependency (0) | 2024.02.08 |