版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39089503/article/details/88553453
一般来说,我们都是这样定义切点的
//表示controller包下的任意返回任意个参数的公共方法
@Pointcut("execution(public * org.zhd.crm.server.controller..*.*(..))")
但是,如果我想要更加灵活,颗粒度更小时,可以使用自定义注解的方式来定义切点。
1.自定义注解
kotlin和java中的写法还是有点区别的,以下表示保留至运行时用于方法的注解:
kotlin:
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class AnnotationTest(val value: String = "")
java:
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface AnnotationTest {
String value() default "";
}
2.aop类(kotlin)
@Aspect
@Service
class WebAopService {
...
@Pointcut("@annotation(org.zhd.crm.server.util.AnnotationTest)")
fun testPoint(){
}
@Before("testPoint()")
fun test1(joinPoint: JoinPoint){
println(">>>${joinPoint.signature},${joinPoint.target.javaClass.name}") //返回方法签名和目标对象的类名
}
@Before("testPoint() && @annotation(test)") //@annotation(test)获取注解对象
fun test2(joinPoint: JoinPoint, test: AnnotationTest){
println(">>>${test.value}") //注解的value值
}
...
}
3.在你想要的方法上增加注解(kotlin)
@AnnotationTest("testAnt")
@GetMapping("testAop")
fun testAopAndAnt(): String {
return "success"
}
4.结果