Recently, when using the listener, I encountered the problem that spring could not be injected, the code is as follows, this task is always null, the package has obviously been swept away, but it cannot be injected.
The reason is as follows: the Listener listener cannot inject the bean using the @Resource of the Spring container or the method of @Autowired annotation, because, in the web server container,Neither Servlet, Filter, nor Listener are managed by Spring containers,Therefore, we cannot directly use the Spring annotation method in these classes to inject the objects we need。 Here, the entire lifecycle of the servlet is handled by the servlet container. If you put it into a Spring container to create it, the servlet object can be created by the Spring container, but the servlet container may not know that the servlet exists because it is not in its own container. Therefore, the servlet is managed by the web server, not by spring.
Make the following modifications:
Taken from the context of spring, it solved the problem perfectly.
|