如何检索自定义注释字段?
我想实现一个可以应用于类的自定义注释(在应用程序中一次)以启用功能(访问远程资源)。如果将此注释放置在任何配置类上,它将设置整个应用程序的访问。到目前为止并不难(请参见下面的示例),但是我想在@interface
中包括一些定义字段,这些字段将在访问建立过程中使用。
例如,Spring具有非常相似的内容:@enablejparepositories
。访问可以启用到DB,并在包含定义的注释中具有参数。例如:@enablejparepositories(bootstrapmode = bootstrapmode.deferred)
到目前为止,我有:
仅创建我正在使用类似的访问:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(AccessHandlerConfiguration.class)
public @interface EnableAccessHandlerAutoconfigure {
String name() default "";
}
使用它:
@EnableAccessHandlerAutoconfigure{name="yoni"}
@Configuration
public class config {}
AccessHandlerConfiguration
一个包含建立连接的bean的配置类。
我遇到的问题是我不知道如何检索字段名称的值。我应该怎么办?
I would like to implement a custom annotation that could be applied to a class (once inside an app), to enable a feature (Access to remote resources). If this annotation is placed on any config class, it will set the access for the whole app. So far it isn't that hard (see example below), but I want to include some definition fields in the @interface
that will be used in the access establishing process.
As an example, Spring has something very similar: @EnableJpaRepositories
. Access is enabled to the DB, with parameters in the annotation containing definitions. For example: @EnableJpaRepositories(bootstrapMode = BootstrapMode.DEFERRED)
So far, I have:
To create only the access I'm using something like that:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(AccessHandlerConfiguration.class)
public @interface EnableAccessHandlerAutoconfigure {
String name() default "";
}
Using it:
@EnableAccessHandlerAutoconfigure{name="yoni"}
@Configuration
public class config {}
AccessHandlerConfiguration
is a configuration class that contains beans that establish the connection.
The problem I'm having is that I don't know how to retrieve the field name's value. What should I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
经过长时间的研究,我找到了一种方法:Spring的
ApplicationContext
中有一种方法,该方法根据其注释getBeanNamesForAntation
,然后获取注释本身findannAnnotationOnonBean
,然后简单地使用字段getter。结果:
After a long research, I found a way: There is a method in Spring's
ApplicationContext
that retrieves bean names according to their annotationsgetBeanNamesForAnnotation
, then get the annotation itselffindAnnotationOnBean
, and then simply use the field getter.Results:
检索值可能如下:
通过使用以下方式的实际示例配置类扩展我的评论:
这就是您获得名称值的方式,即您接下来要做什么,这取决于您。
Retrieving the value may be accomplished as follows:
To expand on my comment with an actual example configuration class that uses this:
This is how you get the value of name, as to what you are going to do next, that depends on you.