- 一、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毫秒
总结
记录下遇到的小问题,以后也方便回顾,加油!!!



