1. Background
In some business scenarios, after the container is started, you need to deal with operations such as Kafka business registration, data processing, initializing the cache, and so on.
This article focuses on how to perform related processing when the service starts, or when startup is complete.
2, for the above scenario, like the next implementation method
A: Implement the Javaweb-based Servletcontextlistener interface, overriding the contextinitialized () interface
B: Implement the Filter interface under the Javaweb-based Javax.servlet package, overriding the Init (filterconfig filterconfig) interface
C: Inherit the HttpServlet parent class under Javaweb-based servlet package, overriding the init () interface to
D: Implement spring-based applicationlistener<contextrefreshedevent> interface, rewrite onapplicationevent (contextrefreshedevent Event) interface
Summary: Interfaces based on the Javaweb implementation need to be scanned when web. XML is registered to start. Based on spring implementations, the object needs to be registered in the container.
The following is a loading order in which you can see that listener and filter were executed before the servlet.
3, Applicationlistener Source analysis
You can look at the 78996509 first.
Source analysis on subsequent meeting
"Spring Framework"? Perform specific processing and Applicationlistener source analysis at project start-up