Spring filter interceptor AOP difference

tags: Spring interceptor  Spring filter  Spring face  The difference between interceptors and filters

Introduction

These days when I reviewed Spring's AOP, I was a bit curious about the relationship between filters, interceptors, and AOPs, so the records were backed up. When implementing some common logic, many functions can be implemented through filters, interceptors, and AOPs, but different ways have different efficiencies. What is the difference? See the description below.

Front-back interaction basic logic

filter

The filter intercepts the URL

Spring custom filter (Filter) generally has only one method, the return value is void, when the request reaches the web container, it will detect whether the current request address is configured with a filter, and there is a method to call the filter (may be more Filters), then call the real business logic, and the filter task is completed.The filter does not define the execution of the business logic before, after, etc., but only when the request arrives.

Special attention: the input method of the filter method has request, response, FilterChain, where FilterChain is a filter chain, the use is relatively simple, and request, response is associated with the request process, so the request parameters can be filtered and modified, and FilterChain filtering After the chain is executed and the business process is completed, it returns to the filter, and the returned data of the request can also be processed.

Interceptor

The interceptor intercepts the URL

The interceptor has three methods, which are more detailed than the filter, and are executed before and after the interception logic.Spring interceptors have three methods: preHandle, postHandle, afterCompletion. Respectively as follows

Public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) indicates the custom processing before the method corresponding to the intercepted URL

Public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) indicates that the modelAndView has not been rendered at this time, and the method corresponding to the intercepted URL is executed after the custom processing.

Public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) indicates that the modelAndView has been rendered and the custom handler of the interceptor is executed.

AOP (face-oriented)

Face-to-face interception is the metadata of the class (package, class, method name, parameters, etc.)

More detailed than the interceptor, and very flexible, the interceptor can only intercept the URL, and AOP can implement more complex business logic for specific code.. Refer to other blogs for specific types.

Three use scenarios

The functions of the three are similar, but each has its own advantages. From the filter--"interceptor--" section, the interception rules are more and more detailed, and the order of execution is filter, interceptor and slice. In general, the earlier the data is filtered, the less impact it has on the performance of the service. Therefore, when we write relatively common code, we prioritize the filter, then the interceptor, and finally the aop. For example, permission verification, in general, all requests need to do login verification, then you should use the filter to do the verification at the top level; log records, the general log will only log for some logic, and it involves Logging before and after the completion of the business logic, so the use of filters can not be divided into modules, in this case should consider the interceptor, but the interceptor is also based on the URL to do the rule matching, so relatively inconspicuous, so we will consider using AOP AOP can intercept the method level of the code, which is very suitable for the log function.

Intelligent Recommendation

The difference between the Spring interceptor (Interceptor) of the filter (the Filter) of

Popular first explain: Interceptor: Aspect Oriented Programming is in is in your service or a method, before calling a method, or a method call after method such as dynamic proxy is a simple implement...

The difference between Filter, Interceptor and AOP interception

1. filter The filter can intercept the request and response of the method (ServletRequest request, ServletResponse response), and make response-like filtering operations on the request response, such ...

Filter, interceptor, aop interception implementation and difference

Filter, interceptor, aop interception implementation and difference effect achieve 1 Filter direct implementationFilterInterface, rewrite the intercept method, and then@WebFilterAnnotation can be achi...

The difference and connection between filter, interceptor, and AOP

Spring: The difference and connection between filter, interceptor, and AOP Filter Interceptor Spring AOP interceptor Only access to Spring Management Beans (business layer Service) can be intercepted....

The difference between Spring interceptor and filter

An interceptor is an enhancement of functionality by uniformly intercepting requests sent from the browser to the server. Usage scenario: Solving the common problem of the request (garbled problem, pe...

More Recommendation

The difference between Filter and Interceptor in Spring

Interceptor The main role: intercept user requests, processing, such as judging user login status, permission verification, as long as the controller request processing, is throughHandlerInterceptor。 ...

spring interceptor - the difference between the filter

1. Understanding Interceptor: Aspect Oriented Programming at the time of the call in front of your service or a method a method, or a method call after method; such as dynamic proxy is a simple implem...

The difference between Interceptor and Filter in Spring

First, let's explain in common terms: Interceptor : Aspect-oriented programming is to call a method before your service or a method, or call a method after the method. For example, a dynamic proxy is ...

Spring: Filter FILTER, Interceptor Interceptor, and AOP Differences and Contact

Filter filter The filter intercepts the Web Access URL address. Strictly speaking, FILTER is only suitable for use in the Web, depending on the servlet container, using Java's callback mechanism. Filt...

Spring filter interceptor aop use order

1. The above mentioned how log4j2 log records are used. At present, the main function of log4j2 is to record information such as request parameters, parameters, control layers, methods, and usage time...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top