文章

SpringAOP使用

SpringAOP使用

依赖

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>6.0.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>6.0.11</version>
        </dependency>

一、针对具体方法

业务代码

@Component
public class HelloService {
​
    @Log
    public String sayHello(String name){
        String result = "你好:"+name;
​
        System.out.println(result);
//        int i = 10/0;
        int length = name.length();
        return result + "---" + length;
    }
}

切面

​
@Aspect //说明这是切面
@Component //切面也是容器中的组件
public class LogAspect {
​
    //前置通知  增强方法/增强器
    @Before("execution(* com.orbit.aop.service.HelloService.sayHello(..))")
    public void logStart(JoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        System.out.println("前置logStart()==>"+name+"....【args: "+ Arrays.asList(joinPoint.getArgs()) +"】");
    }
​
    //返回通知
    @AfterReturning(value = "execution(* com.orbit.aop.service.HelloService.sayHello(..))",returning = "result")
    public void logReturn(JoinPoint joinPoint,Object result){
        String name = joinPoint.getSignature().getName();
        System.out.println("返回logReturn()==>"+name+"....【args: "+ Arrays.asList(joinPoint.getArgs()) +"】【result: "+result+"】");
    }
​
​
    //后置通知
    @After("execution(* com.orbit.aop.service.HelloService.sayHello(..))")
    public void logEnd(JoinPoint joinPoint){
        String name = joinPoint.getSignature().getName();
        System.out.println("后置logEnd()==>"+name+"....【args: "+ Arrays.asList(joinPoint.getArgs()) +"】");
    }
​
​
    //异常
    @AfterThrowing(value = "execution(* com.orbit.aop.service.HelloService.sayHello(..))",throwing = "e")
    public void logError(JoinPoint joinPoint,Exception e){
        String name = joinPoint.getSignature().getName();
        System.out.println("异常logError()==>"+name+"....【args: "+ Arrays.asList(joinPoint.getArgs()) +"】【exception: "+e+"】");
    }
}

二、使用注解

注解

@Target({ ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Log {
​
    public String title() default "11";
​
}

切面

@Aspect
@Component
public class LogAspect2 {
​
    /**
     * 处理请求前执行
     */
    @Before(value = "@annotation(controllerLog)")
    public void boBefore(JoinPoint joinPoint, Log controllerLog) {
        System.out.println(controllerLog.title());
        String name = joinPoint.getSignature().getName();
        System.out.println("前置logStart()==>"+name+"....【args: "+ Arrays.asList(joinPoint.getArgs()) +"】");
    }
​
    /**
     * 处理完请求后执行
     *
     * @param joinPoint 切点
     */
    @AfterReturning(pointcut = "@annotation(controllerLog)", returning = "jsonResult")
    public void doAfterReturning(JoinPoint joinPoint, Log controllerLog, Object jsonResult) {
        System.out.println(controllerLog.title());
        String name = joinPoint.getSignature().getName();
        System.out.println("返回logReturn()==>"+name+"....【args: "+ Arrays.asList(joinPoint.getArgs()) +"】【result: "+jsonResult+"】");
    }
    //后置通知
    @After(value = "@annotation(controllerLog)")
    public void logEnd(JoinPoint joinPoint, Log controllerLog){
        System.out.println(controllerLog.title());
        String name = joinPoint.getSignature().getName();
        System.out.println("后置logEnd()==>"+name+"....【args: "+ Arrays.asList(joinPoint.getArgs()) +"】");
    }
    /**
     * 拦截异常操作
     *
     * @param joinPoint 切点
     * @param e 异常
     */
    @AfterThrowing(value = "@annotation(controllerLog)", throwing = "e")
    public void doAfterThrowing(JoinPoint joinPoint, Log controllerLog, Exception e) {
        System.out.println(controllerLog.title());
        String name = joinPoint.getSignature().getName();
        System.out.println("异常logError()==>"+name+"....【args: "+ Arrays.asList(joinPoint.getArgs()) +"】【exception: "+e+"】");
    }
​
}

三、环绕通知方式

@Aspect
@Component
public class LogAspectAround {
​
​
​
//    @Around(value = "@annotation(controllerLog)")
//    public Object doAfterThrowing(ProceedingJoinPoint joinPoint, Log controllerLog) {
//        System.out.println(controllerLog.title());
//        System.out.println(joinPoint.getArgs());
//        System.out.println("环绕前置通知");
//        Object proceed = null;
//        try {
//            proceed = joinPoint.proceed();
//        } catch (Throwable e) {
//            System.out.println("环绕异常通知");
//            throw new RuntimeException(e);
//        }finally {
//            System.out.println("环绕最终通知");
//        }
//        System.out.println("环绕后置通知");
//        return proceed;
//    }
    /**
     * 11
     * [Ljava.lang.Object;@463fd068
     * 环绕前置通知
     * 你好:zhangsan
     * 环绕异常通知
     * 环绕最终通知
     */
​
    @Around("execution(* com.orbit.aop.service.HelloService.sayHello(..))")
    public Object doAfterThrowing(ProceedingJoinPoint joinPoint) {
        System.out.println(joinPoint.getArgs());
        System.out.println("环绕前置通知");
        Object proceed = null;
        try {
            proceed = joinPoint.proceed();
        } catch (Throwable e) {
            System.out.println("环绕异常通知");
            throw new RuntimeException(e);
        }finally {
            System.out.println("环绕最终通知");
        }
        System.out.println("环绕后置通知");
        return proceed;
    }
}
License:  CC BY 4.0