How to get instances of WebApplicationContext and DispatcherServlet in spring -web-mvc during spring web application execution

I want to get an instance of a WebApplicationContext instance and a DispatcherServlet instance in a function under the controller class.

+6
source share
1 answer

As with Spring 2.5, you can get a link to WebApplicationContext using the @Autowired annotation:

 @Autowired WebApplicationContext applicationContext; 

You can also get the ApplicationContext link by implementing the ApplicationContextAware interface:

 public class YourController implements ApplicationContextAware { ApplicationContext applicationContext; public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } ... } 

But I do not think that there is a way to get a link to an instance of DispatcherServlet or to an instance of any servlet present in your application. There used to be a way to get it using ServletContext.getServlet () , which is now deprecated.

+2
source

Source: https://habr.com/ru/post/957652/


All Articles