我正在使用Kotlin项目编写的Spring-Boot,该项目使用自定义方法级注释foo
,该注释在整个项目中具有称为biz
的字符串属性。
@Foo(biz = "buzz")
是否有一种方法与Spring-Boot或Kotlin获得foo
的biz
属性中使用的所有值的列表?
在ListableBeanFactory
上有一个函数,它可以用给定的注释检索所有bean,并返回带有bean的映射-键是bean名称和值bean实例。芬兰湾的科特林的例子:
@Configuration
class AnnotatedBeansCounterConfig(
context: AbstractApplicationContext
) {
init {
context.getBeansWithAnnotation(Foo::class.java).forEach { beanName, bean ->
val annotation = bean::class.findAnnotation<Foo>()
}
}
}```
您可以从上下文中获得所有的spring bean。遵循本教程:
https://www.baeldung.com/spring-show-all-beans
然后您可以使用反射来获取每个bean的注释参数。这里有一个例子:
获取Java注释的参数值