1. Before the web system starts running, you need to populate the database with some pre-built data. Here you need a method that runs only once while the system is running, to implement the initialization process of the system.
Here, it's natural to think of Servletcontextlistener as such a listener, as described in the SERLVET-API documentation.
Interface for receiving notification events about ServletContext lifecycle changes.
and just, a web system, there is only one servletcontext.
2. Then the problem comes, spring initialization, loading the XML configuration is also using this one listener--org.springframework.web.context.contextloaderlistener.
So how do you get the beans through spring applicationcontext in the listener you define?
3. The answer is webapplicationcontextutils. This is a component in the Spring-web, you can use it to get applicationcontext, when you get the ApplicationContext, you naturally have the bean
Java Web (5) on how to get spring beans in listener