开发者

Getting spring servlet application context

开发者 https://www.devze.com 2023-02-20 04:25 出处:网络
I need to get the servlet application context in the business layer of an spring mvc application. I\'ve tried implementing ApplicationContextAware interface, autowiring a WebApplicationContext field,

I need to get the servlet application context in the business layer of an spring mvc application.

I've tried implementing ApplicationContextAware interface, autowiring a WebApplicationContext field, and even implementing ServletContextAware and getting the application context with开发者_C百科 WebApplicationContextUtils, but none of this works.

Is there any other way to get it? I want to avoid having to pass an HttpServletRequest parameter from the controller to get the application context in the business object.

If it helps for something, I'm running on a WebSphere 6.1 server, and I think it has the servlet 2.4 api.


Implementing ApplicationContextAware and injecting ApplicationContext work. So that's the way to go. You must find what other problem stops it from working (how does it not work, actually?)

For example make sure the service class is not instantiated by you using the new operator.

By the way, you cannot and should not get the child application context (defined by dispatcher-servlet) in the service layer. This means that parent would know about child contexts - and it does not. You are thus breaking the layer boundaries. Think of another way of handling this task - for example have the context injected in a controller (it should work there), and then pass the desired arguments to a service.

0

精彩评论

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