[英]How to get the instances of the WebApplicationContext and DispatcherServlet in the spring-web-mvc at the runtime of an spring web application
我想在控制器类下的函数中获取WebApplicationContext和DispatcherServlet实例。
As of Spring 2.5, you can get a reference to WebApplicationContext
using @Autowired
annotation: 从Spring 2.5开始,您可以使用
@Autowired
批注获取对WebApplicationContext
的引用:
@Autowired
WebApplicationContext applicationContext;
You can also obtain a reference of ApplicationContext
by implementing ApplicationContextAware interface : 您还可以通过实现ApplicationContextAware接口来获取
ApplicationContext
的引用:
public class YourController implements ApplicationContextAware {
ApplicationContext applicationContext;
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
...
}
But I don't think there is way to get a reference to the DispatcherServlet
instance or to an instance of any Servlet present in your application. 但是我不认为有办法获得对
DispatcherServlet
实例或应用程序中存在的任何Servlet实例的引用。 Earlier there was a way to obtain it using ServletContext.getServlet() , which is deprecated now. 以前有一种使用ServletContext.getServlet()来获取它的方法,现在不推荐使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.