2010-09-03 58 views
4

我目前在這裏工作的是禁用webapp上下文以在Spring初始化一些bean時啓動(在初始化時調用webservices,因此是那麼可能會崩潰)。在BeanInstantiationException(Tomcat)之後禁用Spring啓動

然而,當一個bean初始化期間拋出任何異常,它看起來像這樣:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'latestAdsRepository' defined in file [...]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [...]: Constructor threw exception; nested exception is java.io.IOException: SHUT DOWN NOW!! 
... 
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [...]: Constructor threw exception; nested exception is java.io.IOException: SHUT DOWN NOW!! 

但是上下文開始,至豆,這是無法啓動的引用都是。毋庸置疑,這會導致各種不良Nullpointer異常。特別是因爲這個bean對web應用程序非常重要。

所以我需要一種方式來明確地告訴Spring,如果一個bean無法初始化,這個webapp已經無法啓動。然而System.exit(1)不是一個選項,因爲這個Tomcat服務器上還有其他的webapps。

任何想法?

+0

看到我的答案在這裏: https://stackoverflow.com/questions/25347593/known-way-to-stop-server-when-spring-context-failed-to-load – 2015-12-02 15:03:43

回答

2

據我所知,默認情況下,當Spring無法實例化一個bean時,如果該異常通過啓動監聽器傳播到servlet容器,則上下文啓動失敗。

您可以使用@Required註釋將某個注射點標記爲必填項。

+0

感謝您的回答,但是使用@Required註解根本沒有幫助。我注意到的是:當上下文啓動時(在無法創建所有bean之後),它會嘗試每次重新創建缺失的bean,它們將被使用。這是一種新的默認行爲嗎?我沒有在config中啓用任何類似的東西。 – matt 2010-09-03 11:56:37

+0

你的豆的範圍是什麼? – Bozho 2010-09-03 12:01:31

+0

他們是默認(單身)範圍 – matt 2010-09-03 12:25:02

0

你是如何加載Spring上下文的?

聽起來像是你可能會做手工(即您所呼叫new ApplicationContext()的地方),而不是你應該使用ContextLoaderListener,使您的上下文自動在應用程序啓動(和關閉過程中應用程序關閉)加載。 Spring上下文啓動期間的異常將傳播到Web應用程序上下文啓動失敗。