2016년 7월 25일 월요일

[Spring DI,XML설정을 분리]XML설정파일을 여러개로(XML기반 DI)분리한 예제, ref bean, local - 스프링실무교육학원

[Spring DI,XML설정을 분리]XML설정파일을 여러개로(XML기반 DI)분리한 예제, ref bean, local - 스프링실무교육학원

ref local은 동일한 XML에서 자바빈을 찾을 때 사용하며 ref bean은 다른 XML 파일에서도 자바빈을 찾을 때 사용한다. 

아래예제는 XML 파일을 분리해 놓고 다른 XML 파일에서 자바빈을 로딩하는 예제이니 참조 바랍니다.

열공하세요~

1.Dog.java

package ojc.spring.twoxml;

public interface Dog {
public void jitda();
}



2. jindo.java

package ojc.spring.twoxml;

import org.springframework.stereotype.Component;

public class Jindo implements Dog{
public void jitda() {
System.out.println("진도진도~");
}
}



3. Pudle.java

package ojc.spring.twoxml;

import org.springframework.stereotype.Component;

public class Pudle implements Dog {
public void jitda() {
System.out.println("푸들푸들~");
}
}


4. 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();
}
}



5. 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();
}
}



6. 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.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>



7. 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.xsd">
<bean id="pudle" class="ojc.spring.twoxml.Pudle"/>
<bean id="jindo" class="ojc.spring.twoxml.Jindo"/>
</beans>


8. 결과

5월 19, 2015 8:35:51 오전 org.springframework.context.support.AbstractApplicationContext prepareRefresh
정보: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@238e0d81: startup date [Tue May 19 08:35:51 KST 2015]; root of context hierarchy
5월 19, 2015 8:35:51 오전 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
정보: Loading XML bean definitions from class path resource [applicationContext.xml]
5월 19, 2015 8:35:51 오전 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
정보: Loading XML bean definitions from class path resource [dog.xml]
5월 19, 2015 8:35:51 오전 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
정보: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@6442b0a6: defining beans [dogManager,pudle,jindo]; root of factory hierarchy
진도진도~
푸들푸들~

댓글 없음:

댓글 쓰기