开发者

spring mvc: detect if an exception has been thrown while loading the context

I have a spring mvc application and we run tests with it using jetty.

Sometimes the loading of the context totally fails, because bad xml or because Class Not Found exception or because a bean throws an exception in the constructor, setter or init method.

In such a case I would like to to stop the开发者_高级运维 application with System.exit(1) or take some other drastic measure and not have the test cycle continue any further.

How can that be done?


How are you loading the context? Via context listener? Define your own (extend ContextLoaderListener and override contextInitialized()) and invoke System.exit(1) when context initialization fails:

@Override
public void contextInitialized(ServletContextEvent event) {
    try {
      super.contextInitialized(event);
    } catch (Throwable T) {
      T.printStackTrace();
      System.exit(1);
    }
}

Update For DispatcherServlet, use the same approach but override the initWebApplicationContext() method:

@Override
protected WebApplicationContext initWebApplicationContext() throws BeansException {
    try {
      super.initWebApplicationContext();
    } catch (Throwable T) {
      T.printStackTrace();
      System.exit(1);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜