aop - How to add arguments to Spring AOP aspect
问题描述
In Kotlin language, I configured a Spring AOP annotation like this:
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class Authenticated(val roles: Array<String>)
... and the aspect class like this:
@Aspect
@Component
class AuthenticationAspect {
@Around("@annotation(Authenticated) && args(roles)", argNames = "roles")
@Throws(Throwable::class)
fun authenticate(joinPoint: ProceedingJoinPoint, roles: Array<String>):Any? {
//.. do stuff
return proceed
}
}
And in my methods I add the annotation like this:
@Authenticated(roles = ["read", "write"])
fun someMethod(msg: Pair) {
// do stuff...
}
The annotation works well without arguments, i.e., the annotated method gets intercepted. But with the argument "roles" it never gets matched and I have no clue why. Any help would be much appreciated.
解决方案
当您使用“&& args(roles)”时,您是在目标方法中查找名为“roles”的参数,而不是在注释中。
您可以尝试将您的方面更改为以下内容:
@Around("@annotation(authenticated))
@Throws(Throwable::class)
fun authenticate(joinPoint: ProceedingJoinPoint, authenticated: Authenticated):Any? {
val roles = authenticated.roles
//.. do stuff
return proceed
}
推荐阅读
- android - 如何为 IntellJ IDE 社区版正确设置 android SDK 路径?
- oracle - 如何找到oracle表的所有分区信息
- actions-on-google - 无法识别的深度链接回退在模拟器中不起作用
- python - HTTP 错误代码:访问 twitter 高级 API 时出现 429
- ios - 如何使用 NSURLSession 从 Objective-C 中的服务器下载 100 张图像
- typescript - 使用带有 html 和超链接的 emailcomposer ionic 发送电子邮件
- google-chrome - 无法在 Chrome 中连接到 Github 的网站,但是当我使用 Firefox 时它可以工作
- php - Cakephp REST API 报告缺少控制器类
- bash - 在 bash 中调用变量时出现语法错误
- android - 使用 selenium 在 Android 上打开 ChromeBrowser