2016-11-09 80 views
2

我使用ContextLoaderListener來推廣春季環境。 在此之前在web.xml我有我的自定義偵聽器pefrorms一些檢查。如果這些檢查失敗,我不希望Spring上下文啓動。如何防止ServletContextListener中發生異常的spring上下文加載?

如何防止Spring上下文加載,如果我有另一個偵聽器中的異常?

我會把一些屬性放在ServletContext中,但我不知道它是如何影響加載的spring ctx的。

這裏是非常類似的問題:Stop deployment when Exception occurs in ServletContextListener

回答

0

這裏是我做過什麼來解決:

在我的聽衆在哪裏進行一些檢查,如果有什麼錯誤發生了,我設置屬性servlet上下文

@Override 
public void contextInitialized(ServletContextEvent sce) { 
sce.getServletContext().setAttribute("checkFailed", true); 
} 

然後我擴展Spring的ContextLoaderListener和overrided contextInitialized這樣的:

@Override 
public void contextInitialized(ServletContextEvent event) { 
    if (event.getServletContext().getAttribute("checkFailed") != null) { 
     return; 
    } 
    super.contextInitialized(event); 
}