栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

多个环绕通知修改值后,前置通知获取值的问题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

多个环绕通知修改值后,前置通知获取值的问题

文章目录
  • 一、Spring学习中遇到的小问题
    • 1、AOP切面类
      • 代码
        • 目标类
        • 前置、返回、异常、后置通知
        • 修改值的切面类
        • 统计运行时间的切面类
        • 运行结果
  • 总结


一、Spring学习中遇到的小问题 1、AOP切面类

当有嵌套切面时,如果使用环绕通知修改属性值,然后再用前置通知获取属性时,会获取第一个执行的环绕通知属性值,也就是说当修改属性值的环绕通知在第一个执行时,前置通知获取的就是修改后的属性值,如果不是第一个执行的话,获取的就是方法本身传入的值。

代码 目标类
package com.zikd.calculator;

import org.springframework.stereotype.Component;



@Component
public class CalculatorImpl{
    
    public int sum(int i, int j) {
        return i + j;
    }

    
    public int sub(int i, int j) {
        return i - j;
    }

    
    public int mul(int i, int j) {
        return i * j;
    }

    
    public int div(int i, int j) {
        return i / j;
    }
}

前置、返回、异常、后置通知
package com.zikd.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;


@Component
@Aspect
public class LogAspect {

    @Pointcut("execution(* com.zikd.calculator.*.*(..))")
    public void la1(){}

    
    @Before("la1()")
    public void printLogBeforeCode(JoinPoint joinPoint) {
        System.out.println("【前置通知日志】:" + joinPoint.getSignature().getName() + "方法执行了...参数为:" + Arrays.toString(joinPoint.getArgs()));
    }

    
    @AfterReturning(value = "la1()", returning = "result")
    public void printLogAfterReturningCode(JoinPoint joinPoint, int result) {
        System.out.println("【返回通知日志】:" + joinPoint.getSignature().getName() + "方法返回值为:" + result);
    }

    
    @AfterThrowing(value = "la1()",throwing = "throwable")
    public void  printAfterThrowing(JoinPoint joinPoint,Throwable throwable){
        System.out.println("【异常通知日志】:" + joinPoint.getSignature().getName() + "方法异常信息为:" + throwable);
    }

    
    @After("la1()")
    public void printAfterCode(JoinPoint joinPoint) {
        System.out.println("【后置通知日志】:" + joinPoint.getSignature().getName() + "方法运行结束了...");
    }
}

修改值的切面类
package com.zikd.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;



@Component
@Aspect
@Order(2)
public class DiscountAspect {

    @Around("com.zikd.aspect.LogAspect.la1()")
    public Object discountPrice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        Object[] args = proceedingJoinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            args[i] = (int) args[i] + 100;
        }
        System.out.println(Arrays.toString(args));
        return proceedingJoinPoint.proceed(args);
    }
}

统计运行时间的切面类
package com.zikd.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;



@Component
@Aspect
@Order(1)
public class AroundAspect {

    @Around("com.zikd.aspect.LogAspect.la1()")
    public Object printRunTimeAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("统计时间方法...");
        long startTime = System.currentTimeMillis();
        try {
            return proceedingJoinPoint.proceed();
        } finally {
            long endTime = System.currentTimeMillis();
            System.out.println("【环绕通知日志:】" + proceedingJoinPoint.getSignature().getName() + "方法运行时间为:" + (endTime - startTime)+"毫秒");
        }
    }
}
运行结果
	统计时间方法...
	[102, 103]
	【前置通知日志】:sum方法执行了...参数为:[2, 3]
	【返回通知日志】:sum方法返回值为:205
	【后置通知日志】:sum方法运行结束了...
	【环绕通知日志:】sum方法运行时间为:2毫秒

总结

记录下遇到的小问题,以后也方便回顾,加油!!!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/842489.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号