1. Concept
The Interceptor in Java is the object that dynamically intercepts action calls, and it provides a mechanism to enable a developer to execute a piece of code before and after an action executes, or to prevent it from executing before an action executes. It also provides a way to extract reusable portions of the code in an action. In AOP, interceptors are used to intercept a method or field before it is accessed, before or after a certain action is added.
At present, we need to master the main spring interceptors, Struts2 interceptors do not have to delve into, know.
2. Interceptor and Filter Difference
- Filter based on the callback function, we need to implement the filter interface in the Dofilter method is the callback function, and interceptor is based on the Java itself reflection mechanism, which is the most essential difference between the two
- The filter relies on the servlet container, while the interceptor does not depend on the servlet container
- Filter filtering range than interceptor, filter in addition to filter requests through wildcards can protect pages, pictures, files and so on, and interceptor can only filter requests.
- The interceptor can access the action context, the object in the value stack, and the filter cannot
- Interceptors can be called multiple times during the life cycle of an action, and filters can only be called once when the container is initialized
3.Spring Interceptor
There are two main interceptors in spring, one is handlerinterceptor and the other is methodinterceptor.
3.1 Handlerinterceptor
Public Interface handlerinterceptor { booleanthrows Exception; void throws Exception; void throws Exception;}
Handlerinterceptor is the interceptor in the SPRINGMVC project, which intercepts the target of the requested address, which is executed before methodinterceptor.
To use the Handlerinterceptor step:
- Implement a Handlerinterceptor interceptor can directly implement the Handlerinterceptor interface, you can also inherit the Handlerinterceptoradapter class.
- Configuring interceptors in the Springmvc configuration file
code example:
Public classHandlerinterceptordemoImplementsHandlerinterceptor {@Override Public BooleanPrehandle (Javax.servlet.http.HttpServletRequest httpservletrequest, Javax.servlet.http.HttpServletResponse HttpServletResponse, Object O)throwsException {System.out.println ("--------Handlerinterceptor.prehandle---------"); return true; } @Override Public voidPosthandle (Javax.servlet.http.HttpServletRequest httpservletrequest, Javax.servlet.http.HttpServletResponse HttpServletResponse, Object O, Modelandview modelandview)throwsException {System.out.println ("--------Handlerinterceptor.posthandle---------"); } @Override Public voidAftercompletion (Javax.servlet.http.HttpServletRequest httpservletrequest, Javax.servlet.http.HttpServletResponse HttpServletResponse, Object O, Exception e)throwsException {System.out.println ("--------handlerinterceptor.aftercompletion---------"); }}
<mvc:interceptors> <!--configuring interceptors here will intercept all requests, and public interceptors can have multiple - <Mvc:interceptor> <!--intercept the/test . - <mvc:mappingPath= "/test"/> <!--the interceptor for a particular request can have only one - <Beanclass= "Com.xzh.HandlerInterceptorDemo" /> </Mvc:interceptor> </mvc:interceptors>
Test results:
3.2 Methodinterceptor
Public Interface extends Interceptor { throws throwable;}
Methodinterceptor is an object interceptor in an AOP project, and it intercepts the method, even if it is not a method in the controller.
To use the Methodinterceptor step:
- Implement the Methodinterceptor interface, or take advantage of ASPECTJ annotations
- Configuring in the Spring configuration file
code example:
@Component public class methodinterceptordemo implements Methodinterceptor {@Override public objec T invoke (Methodinvocation methodinvocation) throws Throwable {System.out.println (-------methodinterceptordemo.invoke--------" = Methodinvocation.getmethod (); System.out.println (-------Method: "+ method.getname () +"-------" return methodinvocation.proceed (); // If the method is not called, the intercepted method will not be executed }}
<Aop:config> <!--entry point - <Aop:pointcutID= "Methodpoint"expression= "Execution (* com.xzh.*controller.* (..)) "/><!--use a custom interceptor in this pointcut - <Aop:advisorPointcut-ref= "Methodpoint"Advice-ref= "Methodinterceptordemo"/> </Aop:config>
Test results:
Reference:
Implementation principles and code examples for the Java three-amp Interceptor (Interceptor)
Talk about the interceptor in spring interceptor
Spring Method Interceptor Methodinterceptor Configuration
Javaweb Study Notes-interceptor