Basically you can isolate your root application context and web application context using ContextLoaderListner.
Listener class - Listens on an event (Eg.. Server startup/shutdown)
ContextLoaderListener -
- Listens during server start up/shutdown
- Takes the Spring configuration files as input and creates the beans as per configuration and make it ready (destroys the bean during shutdown)
ContextLoaderListner is a Servlet listener that loads all the different configuration files (service layer configuration, persistence layer configuration etc) into single spring application context.
This helps to split spring configurations across multiple XML files.
Once the context files are loaded, Spring creates a WebApplicationContext object based on the bean definition and stores it in the ServletContext of your web application.
ContextLoaderListener supports injecting the root web application context via the ContextLoaderListener(WebApplicationContext) constructor, allowing for programmatic configuration in Servlet 3.0+ environments. If we write web.xml without ContextLoaderListener then we cant give the athuntication using customAuthenticationProvider in spring security. Because DispatcherServelet is the child context of ContextLoaderListener, customAuthenticationProvider is the part of parentContext that is ContextLoaderListener. So parent Context cannot have the dependencies of child context. And so it is best practice to write spring-context.xml in contextparam instead of write it in the initparam.
Comments
Post a Comment