我编写了一个customcontextloaderlistener,它会在Web应用程序启动时被调用。
public class CustomContextLoaderListener extends ContextLoaderListener {
private Logger log = Logger.getLogger(CustomContextLoaderListener.class);
public void contextInitialized(ServletContextEvent sce){
WebApplicationContext wctx = new ContextLoader().initWebApplicationContext(sce.getServletContext());
AppContext.setWebApplicationContext(wctx);
wctx.getBeanDefinitionCount();
String [] beanNames = wctx.getBeanDefinitionNames();
for(String beanName : beanNames){
log.info(beanName);
}
现在,如果bean初始化出错,我想停止Web应用程序。所以我想我可以做这样的事情。
public class CustomContextLoaderListener extends ContextLoaderListener {
private Logger log = Logger.getLogger(CustomContextLoaderListener.class);
public void contextInitialized(ServletContextEvent sce){
try {
WebApplicationContext wctx = new ContextLoader().initWebApplicationContext(sce.getServletContext());
AppContext.setWebApplicationContext(wctx);
wctx.getBeanDefinitionCount();
String [] beanNames = wctx.getBeanDefinitionNames();
for(String beanName : beanNames){
log.info(beanName);
}
}catch(Exception e) {
contextDestroyed(sce)
}
public void contextDestroyed(ServletContextEvent sce) {
// not sure how should I stop the web app here..
any ideas
}
答案 0 :(得分:-1)
您可以执行以下操作:
private void callExceptionHandler(Exception exception) {
logger.error("failed to create bean: ", exception);
System.exit(1);
}
您可以从contextInitialized
方法调用上述方法:
catch(Exception e) {
callExceptionHandler(e)
}