Home > database >  How to retrieve custom annotation fields?
How to retrieve custom annotation fields?

Time:05-24

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?

CodePudding user response:

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.

CodePudding user response:

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*******************
  • Related