我编写了一个自定义 contextloaderlistener,当 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
}
你可以做如下的事情:
private void callExceptionHandler(Exception exception) {
logger.error("failed to create bean: ", exception);
System.exit(1);
}
您可以从调用上面的方法contextInitialized
method:
catch(Exception e) {
callExceptionHandler(e)
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)