我遇到了Bean實例化排序很重要的問題。目前下面的Bean3正在運行基於數據庫的緩存放置操作,Bean 1使用代理Bean2針對新創建的緩存進行查詢。在Bean1被實例化之前,即在Spring容器出現之前,Bean3和Bean2的優先級被完全實例化。這些bean在單獨的JARS中,Bean1引用到Bean1中不使用Autowired。相反,服務定位器正在給它一個參考。我們使用Spring 2.5.2,而不是使用XML來實例化bean。任何幫助感謝!Spring bean實例化排序
JAR1(Spring項目)
@Service ("bean3") public class Bean3 implements ApplicationListener { public void onApplicationEvent() { //load data from DB and populate cache } public void getCache(){ //get data from cache }
}
@Service ("bean2") public class Bean2 { @Autowired private Bean3 bean3; private void methodA(){ bean3.getCache(); } }
JAR2(非Spring項目)
public class Bean1{ Bean2 bean2 = SpringServiceLocator.getBean("bean2") public void methodB(){ bean2.methodA(); } }
您使用基於構造函數的自動裝配嗎? – Scott
我們在JAR1中啓用了自動發現模式以進行自動裝配。 – Noosphere
將Bean1依賴於Bean2(或創建一個依賴關係鏈)是否可行?)Bean1的方法何時被調用? –