开发者

How to enable Spring application context in servlets not managed by DispatcherServlet?

The scenario:

  1. Some common beans are defined in context xml files.

  2. DispatcherServlet is mapped to /foo/*.

  3. MyServlet is mapped to /bar/*.

  4. I want to share common beans between Foo classes and Bar classes.

The questions:

  1. Can I get the ApplicationContext for the DispatcherServlet, from MyServlet? (In 开发者_Go百科web.xml, I have defined MyServlet after the DispatcherServlet).

  2. If Q1 is not, how can I get the ApplicationContext from MyServlet? Or, how can I let MyServlet be wired with a specific ApplicationContext?


You can't share the context of one servlet with another, but you can declare a second context which acts as the parent context of the DispatcherServlet. Any beans which you need to access from both servlets should go into this new context.

See section 3.13.4 of the Spring docs for how to set this up.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜