#19.스프링에서 XML 설정 파일의 분리
스프링에서 XML 설정 파일의 분리
XML 설정파일을 여러 개로 분리할 수 있으며 이 경우 ref bean 태그를 사용하여 자바 빈을 로딩 하면 된다. ApplicationContext를 생성할 때 여러 XML 파일명을 문자열 배열로 넣어주면 된다.
[Dog.java]
package ojc.spring.twoxml;
public interface Dog {
public void jitda();
}
[Jindo.java]
package ojc.spring.twoxml;
import org.springframework.stereotype.Component;
public class Jindo implements Dog{
public void jitda() {
System.out.println("진도진도~");
}
}
[Pudle.java]
package ojc.spring.twoxml;
import org.springframework.stereotype.Component;
public class Pudle implements Dog {
public void jitda() {
System.out.println("푸들푸들~");
}
}
[DogManager.java]
package ojc.spring.twoxml;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
public class DogManager {
private Pudle pudle;
private Jindo jindo;
public void setPudle(Pudle pudle) {
this.pudle = pudle;
}
public void setJindo(Jindo jindo) {
this.jindo = jindo;
}
public void walwal() {
this.jindo.jitda();
this.pudle.jitda();
}
}
[DogApp.java]
package ojc.spring.twoxml;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DogApp {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml", "dog.xml"});
DogManager dManager = (DogManager) context.getBean("dogManager");
dManager.walwal();
}
}
[src/main/resources/applicationContext.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.2.xsd">
<bean id="dogManager" class="ojc.spring.twoxml.DogManager">
<property name="pudle">
<ref bean="pudle"></ref>
</property>
<property name="jindo">
<ref bean="jindo"></ref>
</property>
</bean>
</beans>
[src/main/resources/dog.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.2.xsd">
<bean id="pudle" class="ojc.spring.twoxml.Pudle"/>
<bean id="jindo" class="ojc.spring.twoxml.Jindo"/>
</beans>
[결과]
진도진도~
푸들푸들~
댓글 없음:
댓글 쓰기