Spring Boot AOP Tutorial

定义计算执行时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Aspect
@Component
class ExecutedTimeAop {
private val logger = LoggerFactory.getLogger(ExecutedTimeAop::class.java)
@Around("execution(* com.six.god..create(..))")
@Throws(Throwable::class)
fun run(joinPoint: ProceedingJoinPoint): Any? {
var result: Any? = null
val timeMillis = measureTimeMillis {
result = joinPoint.proceed()
}
return result
}
}

作用于com.six.god下所有名字为create()的方法

配合注解使用

定义注解

1
2
3
4
5
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
@Inherited
@MustBeDocumented
annotation class ExecutedTime

定义AOP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Aspect
@Component
class ExecutedTimeAop {
private val logger = LoggerFactory.getLogger(ExecutedTimeAop::class.java)
@Around(value = "@annotation(ExecutedTime)")
@Throws(Throwable::class)
fun run(joinPoint: ProceedingJoinPoint): Any? {
var result: Any? = null
val timeMillis = measureTimeMillis {
result = joinPoint.proceed()
}
return result
}
}