[英]Kotlin extending spring boot annotations
With Kotlin I'm able to save defaults in a class level annotation, eg使用 Kotlin 我可以将默认值保存在 class 级别注释中,例如
@Profile("dev", "staging", "production")
annotation class RemoteProfile
I'm curious if there's a way of achieving something similar with parameter annotations - for example I'd love to be able to do something like:我很好奇是否有一种方法可以通过参数注释实现类似的功能-例如,我希望能够执行以下操作:
fun someHandler(@AccountParam account: Account) {
...
}
Where underneath spring would interpret it as spring 下面的位置会将其解释为
fun someHandler(@RequestAttribute("ACCOUNT") account: Account) {
...
}
The concept you're looking for is meta-annotation .您正在寻找的概念是meta-annotation 。 The reason your first example works has nothing to do with Kotlin and is a feature of Spring, where Spring actively inspects the annotation hierarchy.
您的第一个示例有效的原因与 Kotlin 无关,并且是 Spring 的一个功能,其中 Spring 主动检查注释层次结构。 From the Javadoc for
Profile
:来自
Profile
的 Javadoc :
The
@Profile
annotation may be used in any of the following ways:@Profile
注解可以通过以下任何方式使用:
- …
…
- as a meta-annotation, for the purpose of composing custom stereotype annotations
作为元注释,用于编写自定义构造型注释
The MVC annotations RequestAttribute
and RequestParam
don't support meta-annotation. MVC 注释
RequestAttribute
和RequestParam
不支持元注释。 You could define your own HandlerMethodArgumentResolver
s to support this (it's not terribly complicated, and you could subclass RequestAttributeMethodArgumentResolver
), but you can't just meta-annotate in general.您可以定义自己的
HandlerMethodArgumentResolver
来支持这一点(它不是非常复杂,并且您可以RequestAttributeMethodArgumentResolver
),但通常不能只是元注释。
(Groovy's AnnotationCollector
does support this, but it's implemented by a compile-time "unrolling", and I don't believe Kotlin has an equivalent feature.) (Groovy 的
AnnotationCollector
确实支持这一点,但它是通过编译时“展开”实现的,我不相信 Kotlin 具有等效功能。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.