SpringBoot Aop通知

Spring Boot的AOP环境准备

pom.xml中引入相应的依赖模块

<!-- AOP依赖模块 -->

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-aop</artifactId>

</dependency>

1.通知分类: 

 @Before: 前置通知, 在方法执行之前执行

 @After: 后置通知, 在方法执行之后执行

 @AfterRunning: 返回通知, 在方法返回结果之后执行

 @AfterThrowing: 异常通知, 在方法抛出异常之后

 @Around: 环绕通知, 围绕着方法执行

关于方法签名 看第五点

2.前置通知

    

3.后置通知:在后置通知中,不能访问目标方法执行的结果

           

4.返回通知

             

         

5.异常通知

        

6.环绕通知

      

      

package com.jason.spring.aop.impl;

import java.util.Arrays;
import java.util.List;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;


//把这个类声明为一个切面
//1.需要将该类放入到IOC 容器中
@Component
//2.再声明为一个切面
@Aspect
public class LoggingAspect {
    
    //声明该方法是一个前置通知:在目标方法开始之前执行 哪些类,哪些方法
    //作用:@before 当调用目标方法,而目标方法与注解声明的方法相匹配的时候,aop框架会自动的为那个方法所在的类生成一个代理对象,在目标方法执行之前,执行注解的方法
    //支持通配符
    //@Before("execution(public int com.jason.spring.aop.impl.ArithmeticCaculatorImpl.*(int, int))")
    @Before("execution(* com.jason.spring.aop.impl.*.*(int, int))")
    public void beforeMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("The method " + methodName + " begins " + args);
    }
    
    /**
     * 
     * @Author:[email protected]
     * @Title: afterMethod 
     * @Time:2016年12月1日
     * @Description:  在方法执行后执行的代码,无论该方法是否出现异常
     *
     * @param joinPoint
     */
    @After("execution(* com.jason.spring.aop.impl.*.*(int, int))")
    public void afterMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("The method " + methodName + " end " + args);
    }
    
    /**
     * 
     * @Author:[email protected]
     * @Title: afterReturning 
     * @Time:2016年12月1日
     * @Description:  在方法正常结束后执行代码,放回通知是可以访问到方法的返回值
     *
     * @param joinPoint
     */
    @AfterReturning( value="execution(* com.jason.spring.aop.impl.*.*(..))", returning="result")
    public void afterReturning(JoinPoint joinPoint ,Object result){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method " + methodName + " end with " + result);
    }
    
    /**
     * 
     * @Author:[email protected]
     * @Title: afterThrowting 
     * @Time:2016年12月1日
     * @Description:  在目标方法出现异常时会执行代码,可以访问到异常对象,且,可以指定出现特定异常时执行通知代码
     *
     * @param joinPoint
     * @param ex
     */
    @AfterThrowing(value="execution(* com.jason.spring.aop.impl.*.*(..))",throwing="ex")
    public void afterThrowting(JoinPoint joinPoint, Exception  ex){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method " + methodName + " occurs exceptions " + ex);
    }
    
    /**
     * 
     * @Author:[email protected]
     * @Title: around 
     * @Time:2016年12月1日
     * @Description: 环绕通知需要携带 ProceedingJoinPoint 类型的参数
     *                    环绕通知 类似于  动态代理的全过程
     *                   ProceedingJoinPoint:可以决定是否执行目标方法
     *    环绕通知必须有返回值,返回值即为目标方法的返回值
     *    
     * @param proceedingJoinPoint
     */
    @Around("execution(* com.jason.spring.aop.impl.*.*(..))")
    public Object around(ProceedingJoinPoint proceedingJoinPoint){
        
        Object result = null;
        String methodName = proceedingJoinPoint.getSignature().getName();
        
        //执行目标方法
        try {
            //前置通知
            System.out.println("The method " + methodName + "begin with" + Arrays.asList(proceedingJoinPoint.getArgs()));
            
            result = proceedingJoinPoint.proceed();
            
            //后置通知
            System.out.println("The method " + methodName + "end with" + result);
            
        } catch (Throwable e) {
            //异常通知
            System.out.println("The method occurs exception : " + e);
            throw new RuntimeException();
        }
            //后置通知
            
        System.out.println("The method " + methodName + "end with" + result);
        
        return result;        
    }
}

猜你喜欢

转载自blog.csdn.net/qq_25293281/article/details/81531341