我有2個豆與@Scope(值= WebApplicationContext.SCOPE_APPLICATION,proxyMode = ScopedProxyMode.TARGET_CLASS)如何在Spring上重新創建Singleton bean(如果Singleton崩潰)?
這2個bean是單身人士。有時它們會根據併發請求的數量而崩潰。如果他們崩潰了,我確認他們有一個空值,我如何重新創建它們並刷新上下文?或者它會很危險,緩慢並容易出現進一步的錯誤?
此外,什麼是創建一個單身的最好方法,使它更穩定?通過工廠?
我有我的豆RootContextConfiguration類中聲明如下:
@Scope(value = WebApplicationContext.SCOPE_APPLICATION, proxyMode = ScopedProxyMode.TARGET_CLASS)
public MyFinderModels myFinderModels() {
MyFinderModels myFinderModels = new MyFinderModels();
return myFinderModels;
}
這是對我的RootContextConfiguration類中聲明
。
@Configuration
@EnableScheduling
@EnableLoadTimeWeaving
@EnableAsync(
mode = AdviceMode.PROXY, proxyTargetClass = true,
order = 1
)
@ComponentScan(basePackages = {"com.mydomain.api.service","com.mydomain.api.repositories"}, excludeFilters = @ComponentScan.Filter({
Controller.class, ControllerAdvice.class }))
@ImportResource({ "classpath:com/mydomain/config/securityConfiguration.xml" })
public class RootContextConfiguration implements AsyncConfigurer,
SchedulingConfigurer { .....
如果Bean MyFinderModels崩潰,它將變爲null。因此,在這種情況下最好的辦法是什麼?在ApplicationContext級別重新創建它,還是應該有一個更好的工廠來初始化bean並在發生崩潰時處理它以便恢復它?
它不會每次都崩潰,但是當它需要重新啓動我的容器時。
我正在使用Spring版本4.2.3.RELEASE。
謝謝
C.
一個bean崩潰意味着什麼? – MeBigFatGuy
春豆本身不會崩潰。因此,如果您有任何問題,請將代碼顯示在發生問題的位置(即,您的bean將爲空的位置)。 – dunni