我們使用@Configuration
類來執行基於Java的Spring配置。我正在嘗試設置AnnotationConfigApplicationContext
(s)的層次結構。基於註解的配置層次結構
它似乎工作。正如我可以從父上下文自動裝入Bean作爲從其中一個子上下文創建的bean的成員。
但是我沒有管理從父上下文自動連接Bean到@Configuration
類文件,這非常方便。他們都是空的。
// parent context config
@Configuration
public class ParentContextConfig{
@Bean parentBeanOne...
@Bean parentBeanTwo...
}
// child context config
@Configuration
public class ChildContextConfig{
@Autowired parentBeanOne
@Bean childBeanOne...
}
// a sample bean
@Component
public class ChildBeanOne{
@Autowired parentBeanTwo
}
在此示例中,我所得到的是parentBeanTwo
而parentBeanOne
沒有自動裝配(null
)到配置文件正確創建。
我錯過了什麼?
您是如何設置父母/子女關係的? – skaffman 2011-03-21 10:26:24
我發現如果在父上下文中將這些AnnotationConfigApplicationContext聲明爲bean,則自動裝配到ChildContexts的作品將發揮作用。不過,我開始獲得「循環參考?」相關的例外...我無法識別任何循環引用。 – Rafael 2011-03-21 11:42:19
我在註冊@configurable配置類之前通過執行setParent(ctx)來設置關係。 – Rafael 2011-03-21 11:43:26