0

I try to follow answers to this question, but still cannot make it work. I have following Java config classes:

public class MyWebAppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext container) throws ServletException {
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
          rootContext.register(WebAppConfiguration.class);
          container.addListener(new ContextLoaderListener(rootContext));
          AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();  
          ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
          dispatcher.setLoadOnStartup(1);
          dispatcher.addMapping("/");
    }
}

And for security:

public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
    public SecurityWebApplicationInitializer() {
        super(WebSecurityConfiguration.class);
    }
}

The problem is I get exception like that on startup:

java.lang.IllegalStateException: Cannot initialize context because there is already a root application context present - check whether you have multiple ContextLoader* definitions in your web.xml!

Without SecurityWebApplicationInitializer class it starts up and works fine except of the fact that Spring Security doesn't work. I understand that the problem is that SecurityWebApplicationInitializer injects it's own context. But then what is the right way to make the Spring Security work for my case? Thank you.

Community
  • 1
  • 1
Battle_Slug
  • 2,055
  • 1
  • 34
  • 60

0 Answers0