Spring AOP intercepts pointcut expressions for custom annotations

Source: Internet
Author: User
Tags aop throwable

@within (com.cxh.study.aop.controller.UserAccessAnnotation)
means to intercept all the methods in the class containing the com.cxh.study.aop.controller.UserAccessAnnotation annotation.

@annotation (com.cxh.study.aop.controller.UserAccessAnnotation)
means to intercept the method that contains this annotation

Note: If the annotation is required on a method parameter, the class name is not written in the expression, but the parameter name is written; If there is an annotation on the method of interception, it can be obtained directly by adding parameters to the method, if the annotation is on class it is necessary to use proceedingjoinpoint to get annotations by reflection

Example

Package Com.cxh.study.aop.controller;
Import Org.apache.commons.logging.LogFactory;
Import Org.aspectj.lang.JoinPoint;
Import Org.aspectj.lang.ProceedingJoinPoint;
Import org.aspectj.lang.annotation.*;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.stereotype.Component;

Import Org.springframework.web.servlet.ModelAndView;

Import Javax.servlet.http.HttpServletRequest;
 /** * Created by Jackie on 2015/4/20.  * email:cxh5060@163.com */@Aspect @Component public class Logininterceptor {/** * A Join point is defined in
     The action layer where the method needs * a permission check.

    */@Autowired private httpservletrequest httpservletrequest;
     @Pointcut ("@annotation (com.cxh.study.aop.controller.UserAccessAnnotation)") public void UserAccess () {}/** * Here you need to get useraccessannotation, * If the annotation is on the method, you can get it if the annotation is on the class is null * @param proceedingjoinpoint * @param USERACC Essannotation * @throws ThRowable */@Around (value = "@within (useraccessannotation) | |" + "@annotation (useraccessannotation)") public void Beforemethod (Proceedingjoinpoint proceedingjoinpoint, Useraccessannotation useraccessannotation) throws
        throwable{System.out.println ("method starts");
        Proceedingjoinpoint.proceed (Proceedingjoinpoint.getargs ());

    Proceedingjoinpoint.gettarget (). GetClass (); }/** * There is no need to get useraccessannotation * @param proceedingjoinpoint * @throws throwable * */@Arou nd (value = "@within (com.cxh.study.aop.controller.UserAccessAnnotation) | |" + "@annotation (com.cxh.study.aop.c Ontroller.
        useraccessannotation) public void BeforeMethod1 (Proceedingjoinpoint proceedingjoinpoint) throws throwable{
        System.out.println ("method starts");
        Proceedingjoinpoint.proceed (Proceedingjoinpoint.getargs ());

    Proceedingjoinpoint.gettarget (). GetClass (); } @AfterReturning ("UserAccess ()") public void Aftermethod () {System.out.println (" method ends "); } @Before (value = "@within (com.cxh.study.aop.controller.UserAccessAnnotation) | | @annotation (com.cxh.study.aop.controller.UserAccessAnnotation) ") public void before (Joinpoint joinpoint) throws Throwable {Logfactory.getlog (This.getclass ()). info ("Monitor.before, Class:" + Joinpoint.getsignature (). getDeclar
    Ingtype (). Getsimplename () + ", Method:" + joinpoint.getsignature (). GetName ()); } @After (value = "@within (com.cxh.study.aop.controller.UserAccessAnnotation) | | @annotation (com.cxh.study.aop.controller.UserAccessAnnotation) ") public void After (Joinpoint joinpoint) throws Throwable {Logfactory.getlog (This.getclass ()). info ("Monitor.after, Class:" + Joinpoint.getsignature (). Getdeclari
    Ngtype (). Getsimplename () + ", Method:" + joinpoint.getsignature (). GetName ());

 }
}

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.