Main
package com.spring;
import org.springframework.context.support.GenericXmlApplicationContext;
public class main {
public static void main(String[] args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext("classpath:com/config/echo1.xml", "classpath:com/config/echo2.xml");
Person person = (Person) ctx.getBean("person");
System.out.println(person);
System.out.println(person.getUserName());
System.out.println(person.getCat().getCatName()+"\t"+person.getCat().getCatAge());
}
}
Person(DTO)
package com.spring;
public class Person {
private String userName;
private Cat cat;
public Person(String userName, Cat cat) {
super();
this.userName = userName;
this.cat = cat;
}
@Override
public String toString() {
return "Person [userName=" + userName + ", cat=" + cat + "]";
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
}
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">
<bean id="cat" class="cohttp://m.spring.Cat">
<constructor-arg name="catName" value="야옹이"/>
<constructor-arg name="catAge" value="10"/>
</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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- main에서 여러 xml을 받아오면 자동으로 다른 xml의 cat과 연결 -->
<bean id="person" class="com.spring.Person" autowire="byName">
<property name="userName" value="홍길동"/>
<!-- <property name="cat" ref="cat"></property> -->
</bean>
</beans>
Main에서 Echo2를 받아오지만, Echo2에서 사용하는 Cat은 Echo2에 없음
하지만, 자동으로 Echo와 Echo2가 연결되어 Cat도 사용가능
'단순 코드 기록 > Spring' 카테고리의 다른 글
Spring_Value (0) | 2024.02.08 |
---|---|
Spring_@Resource (0) | 2024.02.08 |
Spring_Autowire (0) | 2024.02.08 |
Spring_Scope (0) | 2024.02.08 |
Spring_map (0) | 2024.02.08 |