开发者

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 typic开发者_Python百科al 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜