Javaweb Study Notes-interceptor

Source: Internet
Author: User
Tags aop throwable

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
    1. 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
    2. The filter relies on the servlet container, while the interceptor does not depend on the servlet container
    3. 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.
    4. The interceptor can access the action context, the object in the value stack, and the filter cannot
    5. 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:

    1. Implement a Handlerinterceptor interceptor can directly implement the Handlerinterceptor interface, you can also inherit the Handlerinterceptoradapter class.
    2. 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:

    1. Implement the Methodinterceptor interface, or take advantage of ASPECTJ annotations
    2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.