Error occurred while add a custom Listener : check whether you have multiple ContextLoader* definitions in your web.xml
I have a class
public class MyContextListener extends org.springframework.web.context.ContextLoaderListener
and register it as below:
@Configuration
public class ApplicationConfiguration {
@Bean
public ServletContextListener listener(){
return new ContextDestroyListener();
}
}
But get the following error:
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!
I just want to use the custom ContextLoaderListener, rather than the default ContextLoaderListener. But get this error. It seems when I use the @Bean annotation to register a ContextLoaderListener, the default Spring ContextLoaderListener is still being used, so they are 2 ContextLoader in ApplicationContext, which cause this error. I want to know what should I do, if I want to register my own ContextLoaderListener, and let Sprint not to add the default one automatically.
Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.