springboot中aop的应用场景_SpringBoot项目中使用AOP

1.概述

将通用的逻辑用AOP技术实现可以极大的简化程序的编写,例如验签、鉴权等。Spring的声明式事务也是通过AOP技术实现的。

Spring的AOP技术主要有4个核心概念:

Pointcut: 切点,用于定义哪个方法会被拦截,例如 execution(* cn.springcamp.springaop.service.*.*(..))

Advice: 拦截到方法后要执行的动作

Aspect: 切面,把Pointcut和Advice组合在一起形成一个切面

Join Point: 在执行时Pointcut的一个实例

Weaver: 实现AOP的框架,例如 AspectJ 或 Spring AOP

2. 切点定义

常用的Pointcut定义有 execution 和 @annotation 两种。execution 定义对方法无侵入,用于实现比较通用的切面。@annotation 可以作为注解加到特定的方法上,例如Spring的Transaction注解。

execution切点定义应该放在一个公共的类中,集中管理切点定义。

示例:

public class CommonJoinPointConfig {

@Pointcut("execution(* cn.springcamp.springaop.service.*.*(..))")

public void serviceLayerExecution() {}

}

这样在具体的Aspect类中可以通过 CommonJoinPointConfig.serviceLayerExecution()来引用切点。

public class BeforeAspect {

@Before("CommonJoinPointConfig.serviceLayerExecution()")

public void before(JoinPoint joinPoint) {

System.out.println(" -------------> Before Aspect ");

System.out.println(" -------------> before execution of " + joinPoint);

}

}

当切点需要改变时,只需修改CommonJoinPointConfig类即可,不用修改每个Aspect类。

3. 常用的切面

Before: 在方法执行之前执行Advice,常用于验签、鉴权等。

After: 在方法执行完成后执行,无论是执行成功还是抛出异常.

AfterReturning: 仅在方法执行成功后执行.

AfterThrowing: 仅在方法执抛出异常后执行.

一个简单的Aspect:

@Aspect

@Component

public class BeforeAspect {

@Before("CommonJoinPointConfig.serviceLayerExecution()")

public void before(JoinPoint joinPoint) {

System.out.println(" -------------> Before Aspect ");

System.out.println(" -------------> before execution of " + joinPoint);

}

}

4. 自定义注解

假设我们想收集特定方法的执行时间,一种比较合理的方式是自定义一个注解,然后在需要收集执行时间的方法上加上这个注解。

首先定义一个注解TrackTime:

@Target({ElementType.METHOD, ElementType.TYPE})

@Retention(RetentionPolicy.RUNTIME)

public @interface TrackTime {

String param() default "";

}

然后再定义一个Aspect类,用于实现注解的行为:

@Aspect

@Component

public class TrackTimeAspect {

@Around("@annotation(trackTime)")

public Object around(ProceedingJoinPoint joinPoint, TrackTime trackTime) throws Throwable {

Object result = null;

long startTime = System.currentTimeMillis();

result = joinPoint.proceed();

long timeTaken = System.currentTimeMillis() - startTime;

System.out.println(" -------------> Time Taken by " + joinPoint + " with param[" + trackTime.param() + "] is " + timeTaken);

return result;

}

}

在某个方法上使用这个注解,就可以收集这个方法的执行时间:

@TrackTime(param = "myService")

public String runFoo() {

System.out.println(" -------------> foo");

return "foo";

}

注意 @TrackTime(param = "myService") 注解是可以传参的。

为了让注解可以传参数,需要在定义注解时指定一个参数String param() default "默认值",

同时在Aspect类中,around方法上加上相应的参数,@Around注解中也需要用参数的变量名trackTime,而不能用类名TrackTime。

@Around("@annotation(trackTime)")

public Object around(ProceedingJoinPoint joinPoint, TrackTime trackTime)

5.总结

在运行示例项目时,控制台会输出以下内容:

-------------> Before Aspect

-------------> before execution of execution(String cn.springcamp.springaop.service.MyService.runFoo())

-------------> foo

-------------> Time Taken by execution(String cn.springcamp.springaop.service.MyService.runFoo()) with param[myService] is 8

-------------> After Aspect

-------------> after execution of execution(String cn.springcamp.springaop.service.MyService.runFoo())

-------------> AfterReturning Aspect

-------------> execution(String cn.springcamp.springaop.service.MyService.runFoo()) returned with value foo

可以看出几种 Aspect 的执行顺序依次为 Before After Around AfterReturning(AfterThrowing)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值