Getting spring servlet application context
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.
精彩评论