java web three elements Filter, front Servlet were introduced, then we look at the Listener related knowledge, the main contents of this post for the next SpringBoot environment, and how to customize the Listener registered with the spring container
I. Environment Configuration
1. Project to build
First, we need to build a web project to facilitate the subsequent examples demonstrate servelt registered, you can create a project by spring boot official website, you can also create a maven project, the following configuration in pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</pluginManagement>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot-local</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone-local</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release-local</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
II. Listener Registration
Here comes our Listener refers specifically related java web listener, and Listener Spring itself is not the same. Listener in java web in the knowledge that a point of the servlet specification, not to proceed here in detail. The following describes the four ways in SpringBoot in Servlet Listener
1. WebListener comment
@WebListener
Annotated Servlet3 + annotation providers may be identified as a class The Listener, using a gesture and foregoing Listener, Filter and not much difference
@WebListener
public class AnoContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("@WebListener context 初始化");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("@WebListener context 销毁");
}
}
Because the specification are not annotated WebListener spring, so in order to recognize it, it is necessary to add annotations based on starting@ServletComponentScan
@ServletComponentScan
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}
2. ordinary bean
The second way is to use the Listener as an ordinary spring bean, spring boot will automatically be packaged as ServletListenerRegistrationBean
an object
@Component
public class BeanContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("bean context 初始化");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("bean context 销毁");
}
}
3. ServletListenerRegistrationBean
By java config to take the initiative to a common Listener object, into the ServletListenerRegistrationBean
object, creating the bean object to spring
public class ConfigContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("config context 初始化");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("java context 销毁");
}
}
The above is just an ordinary class definition, the following bean creation is the key point
@Bean
public ServletListenerRegistrationBean configContextListener() {
ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean();
bean.setListener(new ConfigContextListener());
return bean;
}
4. ServletContextInitializer
Here are the main means of actually ServletContext context creation, active Add Filter, Servlet, Listener to which, in order to achieve the effect of an active registered
public class SelfContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("ServletContextInitializer context 初始化");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("ServletContextInitializer context 销毁");
}
}
@Component
public class ExtendServletConfigInitializer implements ServletContextInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
servletContext.addListener(SelfContextListener.class);
}
}
Note ExtendServletConfigInitializer active registered opportunity, when you start adding this Listenrer, so it would be the highest priority
5. Test
Sign up above describes four ways, you can enter into force, in our actual development, you can choose a demand, do not recommend mixing a variety of ways;
After project startup and shutdown, the output log is as follows
II. Other
web series Bowen
- 191122-SpringBoot series of tutorials web articles Servlet registered four postures
- 191120-SpringBoot Web tutorial series of articles open GZIP Data Compression
- 191018-SpringBoot chapter of the series of tutorials web filter Filter Guide extension piece
- 191016-SpringBoot chapter of the series of tutorials web filter Filter Guide
- 191012-SpringBoot series of tutorials custom web articles of exception handling HandlerExceptionResolver
- 191010-SpringBoot global web tutorial series of articles Exception Handling
- 404, 500 exception page 190930-SpringBoot chapter of the series of tutorials web configuration
- 190929-SpringBoot series of articles tutorials web redirection
- 190913-SpringBoot chapter of the return series of tutorials web text, web pages, images operation posture
- 190905-SpringBoot series of tutorials web articles solve the problem of Chinese garbled
- 190831-SpringBoot web tutorial series of articles on how to customize parameter parser
- 190828-SpringBoot series of tutorials web articles Post request parameter parsing posture Summary
- 190824-SpringBoot series of tutorials web articles Get request parameter parsing posture Summary
- 190822-SpringBoot chapter of the series of tutorials web environment to build Beetl
- 190820-SpringBoot chapter of the series of tutorials web environment to build Thymeleaf
- 190816-SpringBoot chapter of the series of tutorials web environment to build Freemaker
- websocket the advanced part of the WEB 190421-SpringBoot instructions
- 190327-Spring-RestTemplate urlencode parameter analysis of abnormal full analysis
- 190317-Spring MVC-based web application of java config xml configuration without building
- 190316-Spring MVC-based web application to build the xml configuration
- 190213-SpringBoot file upload prompts anomalies The temporary upload location xxx is not valid
Source Project
- Project: https://github.com/liuyueyi/spring-boot-demo
- Project: https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-boot/212-web-listener
1. A gray Blog
Believe everything the book is not as good, above, is purely one of the words, due to limited personal capacity, it is inevitable omissions and mistakes, such as find a bug or have better suggestions are welcome criticism and generous gratitude
Here a gray personal blog, recording all study and work in the blog, welcome to go around
- A gray Blog personal blog https://blog.hhui.top
- A gray Blog-Spring thematic blog http://spring.hhui.top