Spring 다중 applicationContext 중첩사용

spring에서는 applicationContext에 사용되는 spring xml파일을 여러개로 분리하여 관리할 수 있다.


[app-context-child.xml]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?xml version="1.0" encoding="UTF-8"?>
        
    <!-- b는 parent에만 정의되어 있기 때문에 parent의 b가 주입 -->
    <!-- bean 주입 우선순위 local->parent -->
    <bean id="target1" class = "com.ljs.bean.Target">
        <property name="val">
            <ref bean="b"/>
        </property>
    </bean>
    
    <bean id="target2" class = "com.ljs.bean.Target">
        <property name="val">
            <!-- a는 모두 정의 되어 있지만, ref local 이기 때문에 local에서만 찾음 -->
            <ref local="a"/>
        </property>
    </bean>
    
    <bean id="target3" class = "com.ljs.bean.Target">
        <property name="val">
            <!-- a는 모두 정의 되어 있지만, ref parent 이기 때문에 parent 에서만 찾음 -->
            <ref parent="a"/>
        </property>
    </bean>
    
 
    <bean id="a" class = "java.lang.String">
        <constructor-arg><value>a in child</value></constructor-arg>
    </bean>
</beans>
cs


[app-context-parent.xml]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="UTF-8"?>
        
    <bean id="a" class = "java.lang.String">
        <constructor-arg><value>a in parent</value></constructor-arg>
    </bean>
    
    <bean id="b" class = "java.lang.String">
        <constructor-arg><value>b in parent</value></constructor-arg>
    </bean>
        
</beans>
cs



[Target class ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.ljs.bean;
 
public class Target {
    
    private String val;
 
    public String getVal() {
        return val;
    }
 
    public void setVal(String val) {
        this.val = val;
    }
    
}
cs


[main Class]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.ljs;
 
import org.springframework.context.support.GenericXmlApplicationContext;
 
import com.ljs.bean.Target;
 
public class ApplicationMain {
 
    public static void main(String[] args) {
        // ApplicationContext 부트스트랩
        GenericXmlApplicationContext parentCtx = new GenericXmlApplicationContext();
        parentCtx.load("classpath:META-INF/spring/app-context-parent.xml");
        parentCtx.refresh();
        
        GenericXmlApplicationContext childCtx = new GenericXmlApplicationContext();
        childCtx.load("classpath:META-INF/spring/app-context-child.xml");
        childCtx.setParent(parentCtx);
        childCtx.refresh();
    
        /*
         * <property name="val"><ref bean="b"/></property>
         * 원칙 : ref bean이 bean참조 우선순위 local -> parent
         * 결과 : local에는 b가 없어 parent의 b를 참조로 가져옴
         */
        Target target1 = childCtx.getBean("target1", Target.class); 
                
        /*
         * <property name="val"><ref local="a"/></property>
         * 원칙 : ref local은 local xml에 정의된 bean만 참조함
         * 결과 : b는 양쪽 모두 존재하지만 local만 참조하여 child의  a 참조
         */
        Target target2 = childCtx.getBean("target2", Target.class);
        
        /*
         * <property name="val"><ref parent="a"/></property>
         * 원칙 : ref parent은 parent xml에 정의된 bean만 참조함
         * 결과 : b는 양쪽 모두 존재하지만 parent만 참조하여 child의  a 참조
         */
        Target target3 = childCtx.getBean("target3", Target.class);
 
        
        System.out.println(target1.getVal());
        System.out.println(target2.getVal());
        System.out.println(target3.getVal());
        
        childCtx.close();
        parentCtx.close();
        
    }
}
cs


1. xml파일별로 각각 applicationContext객체를 선언하고 xml파일을 로딩한다.

2. 자식이 되는 applicationContext에 setParent 메서드를 통해 부모 applicationContext를 지정한다.

3. <ref>태그는 아래와 같은 순서로 동작한다.

<ref bean=""/>   : 현재 자기자신의 context에 지정된 bean을 먼저 찾고 없으면 부모context를 참조한다.

<ref local=""/>   : 자기자신의 context만 참조한다.

<ref parent=""/> : 부모context만 참조한다


이 글을 공유하기

댓글

Email by JB FACTORY