Filter
Dependency and servlet containers play a role in almost all requests. Initialization of init method is used at container startup, and then doFilter() is called for each request. The main purpose is to filter character encoding and make some business logic judgments. Its working principle is that it starts with the start of your web application, initializes only once, then intercepts related requests, and destroys them only when your web application stops or redeploys.
Application of SpringBook
Interceptor definition:
//The registry name is customFilter and the intercepted url is all @WebFilter(filterName="customFilter",urlPatterns={"/*"}) @Slf4j public class CustomFilter implements Filter{ @Override public void init(FilterConfig filterConfig) throws ServletException { log.info("filter Initialization"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub log.info("doFilter Request processing"); //Pretreatment of request and response // For example, set the request code // request.setCharacterEncoding("UTF-8"); // response.setCharacterEncoding("UTF-8"); //TODO Business Logic //Links are passed directly to the next filter chain.doFilter(request, response); } @Override public void destroy() { log.info("filter Destruction"); } }
Then add the @ServletComponentScan annotation to the startup class.
@SpringBootApplication @ServletComponentScan @Slf4j public class Chapter7Application { public static void main(String[] args) { SpringApplication.run(Chapter7Application.class, args); log.info("chapter7 Service startup"); } }
Monitor
Listener is a special class defined in the servlet specification. Used to listen for creation and destruction events of domain objects such as servletContext, HttpSession and servletRequest. Listen for events where the properties of domain objects are modified. It is used to do some necessary treatment before and after the occurrence of an event. Usually it is to obtain the number of people online and other business needs.
Application of SpringBook
Definition of listener:
@WebListener @Slf4j public class Customlister implements ServletRequestListener{ @Override public void requestDestroyed(ServletRequestEvent sre) { log.info("Monitor: Destruction"); } @Override public void requestInitialized(ServletRequestEvent sre) { log.info("Listener: Initialization"); } }
Just like creating filters, add @ServletComponentScan to the startup class for automatic registration.
Interceptor
Aspect-oriented programming is to call a method in front of your service or a method, or to call a method after a method, such as dynamic proxy is a simple implementation of interceptor, pre-notification and post-notification in spring's aop.
Application of SpringBook
Interceptor definition:
@Slf4j public class CustomHandlerInterceptor implements HandlerInterceptor{ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { log.info("preHandle:Call before request"); //Returning false requests interruption return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { log.info("postHandle:Call after request"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { log.info("afterCompletion:Callback method after request completion, that is, callback after view rendering is completed"); } }
Register the interceptor by inheriting WebMvc Configurer Adapter:
@Configuration public class WebMvcConfigurer extends WebMvcConfigurerAdapter{ @Override public void addInterceptors(InterceptorRegistry registry) { //Registered Interceptor Interception Rules //When multiple interceptors are added in this order of execution, in the order of addition registry.addInterceptor(getHandlerInterceptor()).addPathPatterns("/*"); } @Bean public static HandlerInterceptor getHandlerInterceptor() { return new CustomHandlerInterceptor(); } }
Filters, listeners and interceptors are common global processing components in our web services. We need to understand their principles, mechanisms and usage. I hope this article can help you!