如何检索自定义注释字段?

发布于 2025-01-31 04:43:38 字数 803 浏览 3 评论 0原文

我想实现一个可以应用于类的自定义注释(在应用程序中一次)以启用功能(访问远程资源)。如果将此注释放置在任何配置类上,它将设置整个应用程序的访问。到目前为止并不难(请参见下面的示例),但是我想在@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

思慕 2025-02-07 04:43:38

经过长时间的研究,我找到了一种方法:Spring的ApplicationContext中有一种方法,该方法根据其注释getBeanNamesForAntation ,然后获取注释本身findannAnnotationOnonBean,然后简单地使用字段getter。

@Configuration
public class AccessHandlerConfiguration {

    private final ApplicationContext applicationContext;

    public AccessHandlerConfiguration(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;        
        String[] beansWithTheAnnotation = applicationContext.getBeanNamesForAnnotation(EnableRabbitAutoconfigure.class);

        for (String beanName : beansWithTheAnnotation) {
            EnableRabbitAutoconfigure annotationOnBean = applicationContext.findAnnotationOnBean(beanName, EnableRabbitAutoconfigure.class);
            System.out.println("**********" + beanName + "*********************" + annotationOnBean.name() + "*******************");
        }
    }
}

结果:

**********config*********************yoni*******************

After a long research, I found a way: There is a method in Spring's ApplicationContext that retrieves bean names according to their annotations getBeanNamesForAnnotation, then get the annotation itself findAnnotationOnBean, and then simply use the field getter.

@Configuration
public class AccessHandlerConfiguration {

    private final ApplicationContext applicationContext;

    public AccessHandlerConfiguration(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;        
        String[] beansWithTheAnnotation = applicationContext.getBeanNamesForAnnotation(EnableRabbitAutoconfigure.class);

        for (String beanName : beansWithTheAnnotation) {
            EnableRabbitAutoconfigure annotationOnBean = applicationContext.findAnnotationOnBean(beanName, EnableRabbitAutoconfigure.class);
            System.out.println("**********" + beanName + "*********************" + annotationOnBean.name() + "*******************");
        }
    }
}

Results:

**********config*********************yoni*******************
再见回来 2025-02-07 04:43:38

检索值可能如下:

this.getClass().getAnnotation(EnableAccessHandlerAutoconfigure.class).name()

通过使用以下方式的实际示例配置类扩展我的评论:

@EnableAccessHandlerAutoconfigure(name="yoni")
@Configuration
public class SomeConfiguration {
    @Bean
    SomeBean makeSomeBean() {
        return new SomeBean(this.getClass().getAnnotation(EnableAccessHandlerAutoconfigure.class).name());
    }
}

这就是您获得名称值的方式,即您接下来要做什么,这取决于您。

Retrieving the value may be accomplished as follows:

this.getClass().getAnnotation(EnableAccessHandlerAutoconfigure.class).name()

To expand on my comment with an actual example configuration class that uses this:

@EnableAccessHandlerAutoconfigure(name="yoni")
@Configuration
public class SomeConfiguration {
    @Bean
    SomeBean makeSomeBean() {
        return new SomeBean(this.getClass().getAnnotation(EnableAccessHandlerAutoconfigure.class).name());
    }
}

This is how you get the value of name, as to what you are going to do next, that depends on you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文