Spring-BootSpringBoot极简教程 · Spring Boot Spring Boot 核心技术

Spring AOP 实现函数执行时间打印

2019-07-01  本文已影响2人  北冥Master

AOP 概念

AOP:Aspect Orient Programming
中文翻译:面向切面编程
作为面向对象编程的一种补充,采用非侵入式技术手段处理系统中分布于各个模块的横切关注点,比如事务管理,安全审计,日志、缓存等等。

AOP 优缺点

优点:原有代码无需更改,不影响原有业务逻辑;
缺点:通过代理方式,会有一定的性能损失;

AOP 代理

AOP 的底层实现是通过代理和反射技术,拦截正常的方法调用,在正常方法调用完成前和完成后插入切面相关的代码。具体实现有如下两种:

AOP 关键点:

AOP 实现举例:(函数执行时间打印)

基于常用的 Spring AOP 为例,基于注解实现打印函数执行时间的切面功能。

增加依赖包

创建 Spring 项目,在 pom.xml 加入 Spring AOP 相关的类。

#pom.xml

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aop</artifactId>
  <version>${spring.version}</version>
</dependency>

<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjrt</artifactId>
  <version>1.8.14</version>
</dependency>
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.8.14</version>
</dependency>
开启切面能力

proxyTargetClass = true,强制使用 CGLIB 代理,如果不设置针对于接口方法织入,默认使用 JDK Proxy 代理。

@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
 public class AspectJConfiguration {
 }
定义注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CallTime {

}
创建切面
@Aspect
@Component
public class CallTimeAspect {
 
}
增加切点
@Aspect
@Component
public class CallTimeAspect {
  @Pointcut("@annotation(CallTime) && execution(public org.test.service.*(..))") 
  public void pointCutMethod();
}

"@annotation(CallTime) && execution(public org.test.service.*(..))"
这个表达式表示只拦截加有CallTime注解的,并且是public方法,并且是 org.test.service这个包下面的类的方法,只有匹配这个表达式的方法才会被拦截,当然这个表达式可以更复杂,比如针对参数做匹配等等。

增加连接点和动作

Spring 只支持方法连接点,主要有3种方式对方法进行增强:
@Before 方法执行前;
@After 方法执行后;
@Around 方法执行过程中;

@Aspect
@Component
public class CallTimeAspect {
  @Pointcut("@annotation(CallTime) && execution(public org.test.service.*(..))") 
  public void pointCutMethod();

  @Around("pointCutMethod()")
  public Object callTime(ProceedingJoinPoint joinPoint) throws Throwable {
    long start = System.currentTimeMillis();
    Object proceed = joinPoint.proceed();
    long end = System.currentTimeMillis();
    String className = proceed.getSignature().getDeclaringType().getSimpleName();
    String functionName = proceed.getSignature().getName();
    System.out.println(String.format("%s.%s takes %dms", className, functionName, end - start));
    return proceed;
  }
}
增加拦截代码
# org.test.service

public class BusinessService {

   @CallTime
   void doBusiness() {
      ...
   }
}

当doBusiness在外部被调用时,就会打印执行时间了:

BusinessService.doBusiness takes XXXms
潜在的问题
class Test {
  @CallTime
  public void funcA() {
     this.funcB();
  }
  
  @CallTime
  public void funcB() {
  }

在外部调用:

new Test().funcA()

只有 funcA 会被拦截,funcB 并不会。

Spring AOP 参考

https://www.journaldev.com/2583/spring-aop-example-tutorial-aspect-advice-pointcut-joinpoint-annotations

上一篇 下一篇

猜你喜欢

热点阅读