Filter and servlet, the three elements of java web, are introduced respectively. Next, let's take a look at the related knowledge points of Listener. The main content of this blog is how to customize the Listener and register it to the spring container in the spring boot environment
<!-- more -->
I. environment configuration
1. Project construction
First of all, we need to build a web project to facilitate the demonstration of the following servelt registration examples. You can create a project through the spring boot official website, or you can build a maven project. In pom.xml, the configuration is as follows
<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
The Listener we mentioned here refers to the Listener related to java web, which is different from the Listener of Spring itself. In java web, the knowledge point of Listener is the set of servlet specifications, which will not be expanded in detail here. Here are four ways to use Servlet Listener in Spring boot
1. WebListener annotation
@WebListener annotation is the annotation provided by Servlet3 +, which can identify a class as Listener. The usage gesture is not too different from the previous Listener and Filter
@WebListener public class AnoContextListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("@WebListener context Initialization"); } @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("@WebListener context Destruction"); } }
Because WebListener annotation is not the specification of spring, in order to identify it, you need to add annotation @ ServletComponentScan on the startup class
@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 a normal spring bean, and spring boot will automatically wrap it as a ServletListenerRegistrationBean object
@Component public class BeanContextListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("bean context Initialization"); } @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("bean context Destruction"); } }
3. ServletListenerRegistrationBean
Use java config to actively insert a common Listener object into the ServletListenerRegistrationBean object and create it as a spring bean object
public class ConfigContextListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("config context Initialization"); } @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("java context Destruction"); } }
The above is just a common class definition, and the following bean creation is the key point
@Bean public ServletListenerRegistrationBean configContextListener() { ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean(); bean.setListener(new ConfigContextListener()); return bean; }
4. ServletContextInitializer
This is mainly to actively add Filter, Servlet and Listener to the ServletContext context, so as to achieve an active registration effect
public class SelfContextListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("ServletContextInitializer context Initialization"); } @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("ServletContextInitializer context Destruction"); } } @Component public class ExtendServletConfigInitializer implements ServletContextInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { servletContext.addListener(SelfContextListener.class); } }
Note the active registration time of ExtendServletConfigInitializer. The listener is added at startup, so its priority will be the highest
5. test
Four registration methods are introduced above, all of which can take effect. In our actual development, we can choose one on demand, and it is not recommended to mix multiple methods;
After the project is started and shut down, the output log is as follows
II. other
web series blog
- Four postures of Servlet registration in 191122 spring boot series
- Open GZIP data compression in Web part of 191120 spring boot series
- 191018 spring boot series of tutorials web chapter Filter user guide extension
- 191016 spring boot series tutorials Filter guide in web
- HandlerExceptionResolver for custom exception handling in the web part of the 191012 spring boot series
- Global exception handling in the web part of the 191010 spring boot series
- The 404 and 500 abnormal page configuration of the 190930 spring boot series
- Redirection of web part of 190929 spring boot series
- 190913 spring boot series of tutorials: return text, web page, picture operation posture
- 190905 spring boot series of tutorials: how to solve the problem of Chinese scrambling
- How to customize parameter resolver in the web part of 190831 spring boot series
- Post request parameter parsing posture summary of 190828 spring boot series web
- Get request parameter parsing posture summary of 190824 spring boot series web
- 190822 spring boot series of tutorials: building the Beetl environment in the web chapter
- Building the Thymeleaf environment in the web part of the 190820 spring boot series
- 190816 spring boot series of tutorials: Freemaker environment construction in web chapter
- Instructions for using websocket in 190421 spring boot advanced WEB
- 190327 spring resttemplate's urlencode parameter parsing exception full process analysis
- 190317 spring MVC web application construction based on java config without xml configuration
- 190316 spring MVC web application construction based on xml configuration
- The temporary upload location xxx is not valid
Project source code
- Works: https://github.com/liuyueyi/spring-boot-demo
- Item: https://github.com/liuyueyi/spring-boot-demo/tree/master/spring-boot/212-web-listener
1. A grey Blog
The best letter is not as good as the above. It's just a one-of-a-kind remark. Due to the limited personal ability, there are inevitably omissions and mistakes. If you find a bug or have better suggestions, you are welcome to criticize and correct. Thank you very much
Here is a grey personal blog, recording all the blogs in study and work. Welcome to visit
- A grey Blog personal Blog https://blog.hhui.top
- A grey blog spring special blog http://spring.hhui.top