【发布时间】:2018-07-26 03:44:10
【问题描述】:
我已经创建了我的自定义注释:
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Condition {
String value();
}
我想用这个注解来判断是否运行advice,我试试:
@Condition("some.config")
@Around("execution(public * someMethod())")
Object doSomething(ProceedingJoinPoint joinPoint) throws Throwable {
// some logic here
}
@Around("@annotation(condition)")
Object checkCondition(ProceedingJoinPoint joinPoint, Condition condition) throws Throwable {
String property = (String) configuration.getProperty(condition.value());
if (Boolean.valueOf(property)){
return joinPoint.proceed();
} else {
return null;
}
}
当我在其他一些方法上使用@Condition 时,它可以工作,即应用checkCondition,然后根据配置值执行或不执行该方法。对于doSomething 的建议,但它没有得到应用。
【问题讨论】:
-
你是什么意思“不为建议工作”?分享minimal reproducible example代码。
-
我已更新问题以澄清。
-
如果那是您的自定义注解,除了您自己的代码之外一无所知,为什么要应用它?
-
可能是因为这都是我自己的代码?你为什么这么咄咄逼人?我只是 spring-aop 的新手,并试图弄乱它。
-
激进与否,为什么你认为你自己的自定义注释会对一无所知的代码产生任何影响,而无需你编写代码?。 Spring AOP 通知本身也不是 Spring 组件,因此不考虑布线。一般来说,它也可以像 aspectJ 那样工作,否则过于笼统的切入点会进入无限循环。
标签: java spring spring-aop java-annotations