2

I boostrap spring context using ContextLoaderListener. Before this in web.xml I have my custom listener which pefrorms some checks. If those checks fail i do not want Spring context to be started.

How can i prevent Spring context loading if i had an Exception in another listener?

I would put some attribute in ServletContext, but i do not know how it can affect on loading of spring ctx.

Here is the very similar question: Stop deployment when Exception occurs in ServletContextListener

Kirill
  • 6,762
  • 4
  • 51
  • 81

1 Answers1

0

Here is what i did to solve that:

In my listener where i perform some checks, if something wrong happens, i set attribute to servlet context

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

Then i extended Spring's ContextLoaderListener and overrided contextInitialized like this:

@Override
public void contextInitialized(ServletContextEvent event) {
    if (event.getServletContext().getAttribute("checkFailed") != null) {
        return;
    }
    super.contextInitialized(event);
}
Kirill
  • 6,762
  • 4
  • 51
  • 81