简体   繁体   English

如何在Spring Web应用程序运行时在spring-web-mvc中获取WebApplicationContext和DispatcherServlet的实例

[英]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.

相关问题 spring-roo或spring-web-mvc中的只读字段 - Read only fields in spring-roo or spring-web-mvc 没有Spring Web的Web应用程序的ClassPathXmlApplicationContext或WebApplicationContext - ClassPathXmlApplicationContext or WebApplicationContext for Web application without Spring Web Spring如何将WebApplicationContext实例传递给DispatcherServlet构造函数? - How Spring passes instance of WebApplicationContext to DispatcherServlet constructor? 每个网址映射的Spring MVC web.xml的DispatcherServlet实例 - Spring MVC web.xml's DispatcherServlet instances per url-mapping 春季:我希望为Web应用程序创建一个junit测试-需要WebApplicationContext - Spring: I wish to create a junit test for a web application - WebApplicationContext needed org.springframework.web.servlet.DispatcherServlet noHandlerFound:Spring MVC - org.springframework.web.servlet.DispatcherServlet noHandlerFound : Spring MVC 无法将GenericApplicationContext强制转换为WebApplicationContext:Spring Web Flow - GenericApplicationContext cannot be cast to WebApplicationContext : Spring Web Flow 如何在 Spring WebApplicationContext 中在运行时添加 bean 实例? - How to add bean instance at runtime in spring WebApplicationContext? 将Spring Web应用程序部署到JBoss WebApplicationContext异常 - Deploying Spring Web Applicaiton To JBoss WebApplicationContext Exceptions 如何获取运行 Spring Boot (Spring Web MVC) API(运行时)的 JVM 堆大小? - How to get JVM Heap Size of Running Spring Boot (Spring Web MVC) API (runtime)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM