4.2.5. Spring Boot Configuration Servlet,filter,listener,interceptor1. Configuring the Servlet
Some scenarios in development often use servlets, such as user logout . The following shows how to configure .
There are two ways to configure the use of Servlets, one to use annotations, and one to use a configuration class.
There are two ways to configure a servlet:
(1) annotation configuration Servlet
When you write a simple servlet that accesses this servlet, it jumps to the home page and adds an annotation @WebServlet (value = "/configservlet") to the servlet:
add annotations on the application class @ServletComponentScan
Post-launch access test:
Access:
Access results:
Jump Success!
(2) Code configuration Servlet
Add the following code after annotating the annotations with the servlet code above:
Test success!
2. Configure the filter
There are two configuration options for configuring a filter like configuration servlet:
(1) annotation configuration Filter
Write a filter to add an annotation @WebFilter ("/*"):
add annotations on the application class @ServletComponentScan
Start Access:
Access results:
Note: For a more detailed configuration of filter, you can view the configuration properties in note @webfilter ("/*"), such as configuring filter order of filters.
(2) Code configuration Filter
Note Write the configuration code after the annotations above:
Note: Filterregistrationbean. setorder (0); method to set the filter's order in the filter chain.
No more testing!
3. Configure Listener
There are two ways to configure listener with the Servlet, Fiter:
(1) annotation configuration Listener
The code is as follows:
Note: the optional listener for custom configuration listener is like the following table:
add annotations to the Application class:@ServletComponentScan
To start the test:
(2) Code configuration Listener
Note the above annotations and write the code:
No more testing!
4. Configure Interceptor
To write an interceptor:
To write a configuration class:
Test:
To access any available request:
< instance code >
Github:https://github.com/chunyuding/springboot-demo
Https://github.com/chunyuding/SpringBoot-Demo.git
Code Cloud: Https://gitee.com/dingchunyu/SpringBoot-Demo
Https://gitee.com/dingchunyu/SpringBoot-Demo.git
< recommended books >
Baidu Cloud: Http://pan.baidu.com/s/1qYA0Nxi
Spring Boot Reference Tutorial (vi) Spring boot configuration Servlet,filter,listener,interceptor