SpringBoot AOP示例

AOP主要注解:

@Aspect,作用在类上,说明这是一个Aspect切面类。
@Pointcut,用来描述,你需要在哪些类的哪些方法中植入你的代码。
@Adive,与Pointcut配合使用,主要说明在Pointcut标记方法的什么时机执行,执行之前?执行之后?

@Pointcut express(切面表达式)

designators指示器:
匹配方法:execution() 通过什么方式去匹配哪些类的哪些方法(重点掌握)。
匹配注解:@annotation() @args() @within() @target()
匹配包/类型:within()
匹配对象:this() bean() target()
匹配参数:args()

wildcards通配符:

 *  匹配任意数量的字符
 ..  匹配指定类及其子类
 +  匹配任意数的子包或参数

operators运算符:&&与 ||或 !非

5中Advice

@Before
@After
@AfterReturning
@AfterThrowing
@Around

示例代码

@Aspect
@Component
public class HttpAspect {
    private final static Logger logger = LoggerFactory.getLogger(HttpAspect.class);

    @Pointcut("execution(public * com.imooc.controller.GirlController.*(..))")
    public void log(){
    }

    /**
     * 在截取的方法前执行
     * @param joinPoint
     */
    @Before("log()")
    public void doBefore(JoinPoint joinPoint){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        // url
        logger.info("url={}", request.getRequestURL());
        // method
        logger.info("method={}", request.getMethod());
        // ip
        logger.info("ip={}", request.getRemoteAddr());
        // 类方法
        logger.info("class_method={}", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        // 参数
        logger.info("args={}", joinPoint.getArgs());
    }

    /**
     * 获取方法返回参数
     * @param object
     */
    @AfterReturning(returning = "object", pointcut = "log()")
    public void doAfterReturning(Object object){
        logger.info("response={}", object);
    }
}

Advice示例

@Aspect
@Component
public class AdviceAspectConfig {

    /********PointCut********/

    @Pointcut("@annotation(com.vmware.AopExecutionDemo.annotation.AdminOnlyMethod)")
    public void matchAnnotation(){}

    @Pointcut("execution(* com.vmware.AopExecutionDemo.service.*.find*(int))")
    public void matchIntArgs(){}

    @Pointcut("execution(public * com.vmware.AopExecutionDemo.service..*.*(..) throws java.lang.IllegalAccessException)")
    public void matchExecption(){}

    @Pointcut("execution(int com.vmware.AopExecutionDemo.service.*.*(..))")
    public void matchReturn(){}

    /********Advice********/

    /**
     * 获取方法输入的参数
     * @param productId
     */
    @Before("matchIntArgs() && args(productId)")
    public void before(int productId){
        System.out.println("### before ### productId = [" + productId + "]");
    }

    /**
     *  获取方法返回参数
     * @param result
     */
    @AfterReturning(value = "matchReturn()", returning = "result")
    public void after(int result){
        System.out.println("### after ###:" + result);
    }

    /**
     * Before + After + AfterReturning
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around("matchReturn()")
    public Object after(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("### before ###");
        Object result;
        result = joinPoint.proceed(joinPoint.getArgs());
        System.out.println("### after ###" + result);
        return result;
    }   
}

@annotation示例

@Aspect
@Component
public class AnnotationAspectConfig {

    /**
     * 匹配带有 AdminOnlyMethod Annotation的方法
     */
    @Pointcut("@annotation(com.vmware.AopExecutionDemo.annotation.AdminOnlyMethod)")
    public void matchAnnotation(){}

    @Before("matchAnnotation()")
    public void before(){
        System.out.println("###matchAnnotation && Before");
    }
}
@Aspect
@Component
public class AnnotationClassAspectConfig {

    /**
     * 匹配带有 AdminOnlyClass Annotation的类
     */
    @Pointcut("@within(com.vmware.AopExecutionDemo.annotation.AdminOnlyClass)")
    public void annotationClassCondition(){}


    @Before("annotationClassCondition()")
    public void before(){
        System.out.println("###annotationClass && before");
    }
}
上一篇:如何使两台机器不通过密码连接起来(linux)


下一篇:java 自定义注解+AOP实现日志记录