2009-07-07 16 views
3

私はアプリケーションコンテキストの階層を持っています。親コンテキストで定義されているBeanは、その子で定義されているBeanによって異なります。ここではそれがどのように見えるかです:Spring:ネストされたアプリケーションコンテキスト

public class X { 

    public static class A { 
     public B b; 
     public void setB(B b) { this.b = b; } 
    } 

    public static class B { } 

    public static void main(String[] args) { 
     ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext(
      "/a.xml"); 
     go1(parent); 
    } 

    public static void go1(ClassPathXmlApplicationContext parent) { 
     GenericApplicationContext child = new GenericApplicationContext(parent); 

     child.getBeanFactory().registerSingleton("b", new B()); 

     A a = (A) child.getBean("a"); 
     Assert.assertNotNull(a.b); 
    } 
    } 

「」Beanを定義する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-2.5.xsd"> 

    <bean id="a" class="X$A" autowire="byName" lazy-init="true"/> 


    </beans> 

問題は、BがAの注入に注入されていないことにのみI場合に発生します"b"シングルトンを私のプログラムのオプションではない親に登録してください。

アイデア?

答えて

10

これはできません。親コンテキストは、子コンテキスト内のBean定義を参照することはできません。それはまったく逆です。

関連する問題