Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using single spring application context for web app

I'm using org.springframework.web.servlet.DispatcherServlet and org.springframework.ws.transport.http.MessageDispatcherServlet in the same app but each is loading own application context, I need to load all beans in a single application context.

The application consists of typical layers web>app>dao etc

What I have tried is to use one single spring-root-context.xml by setting it in the contextConfigLocation.

But didn't help, this has been an issue for me for a long time an I would appreciate any help with this.

Any online references would be a great help.

like image 563
Ramo Avatar asked Mar 22 '10 11:03

Ramo


1 Answers

What you need here is the ContextLoaderListener. This is a ServletContextListener which creates a root WebApplicationContext that is shared amongst all servlets in that webapp.

Your DispatcherServlet and MessageDispatcherServlet will still create their own contexts, but each will have the root context as their parent, so they'll both be able to use beans defined in that root context, like DAOs etc. Some beans will have to remain in the servlets own contexts, such as controllers, view resolvers, SOAP endpoints, and so on, but the shared common beans can go in the root.

For an example on how to configure this, see the Spring docs.

like image 197
skaffman Avatar answered Sep 21 '22 18:09

skaffman