@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 ());
}
}