Spring context sharing between different modules in a web application

Let’s consider a scenario of a web application which contains multiple modules. Each module represents a different Spring context as each module works almost independent of other modules. With this model, the web application is working properly. So far so good.

Now, as per requirement, we need to implement another module which provides security related functionality which is required by each and every module. So, we go ahead and create a new module say “security” which has its own Spring context. Now, if we want to use services of this module into other modules, we either need to make an HTTP call to the exposed “security” services which is expensive or we need to specify beans of “security” module into context files of other modules too. But this can lead to a serious problem which is the creation of multiple instances of “security” beans, which is not acceptable.

So, we need a way to make these “security” beans, Singleton and make them available globally in the web application. In order to achieve that, Spring provides “Parent Context” feature. As per this feature, we can declare our beans inside parent context which will be available to all the modules declared under that web application at run-time.

Continue reading “Spring context sharing between different modules in a web application”