Search code examples
javaspringspring-annotations

Get values from 'composed Annotations' in Spring


With Spring, you can have some kind of composed annotations. A prominent example is the @SpringBootApplication-annotation, which is a composite of an @Configuration, @EnableAutoConfiguration and @ComponentScan.

I am trying to get all Beans that are affected by a certain annotation, i.e. ComponentScan.

Following this answer, I am using the following code:

for (T o : applicationContext.getBeansWithAnnotation(ComponentScan.class).values()) {
    ComponentScan ann = (ComponentScan) o.getClass().getAnnotation(ComponentScan.class);
    ...
}

which does not work, since not all beans, returned by getBeansWithAnnotation(ComponentScan.class) are indeed annotated with that annotation, since those that are e.g. annotated with @SpringBootApplication are (usually) not.

Now I am looking for some kind of generic way, to retrieve the value of an annotation, even when it is only added as piece of another annotation. How can I do this?


Solution

  • It turns out, there is a utility set AnnotatedElementUtils which allows you to handle those merged annotations.

    for (Object annotated : context.getBeansWithAnnotation(ComponentScan.class).values()) {
        Class clazz = ClassUtils.getUserClass(annotated) // thank you jin!
        ComponentScan mergedAnnotation = AnnotatedElementUtils.getMergedAnnotation(clazz, ComponentScan.class);
        if (mergedAnnotation != null) { // For some reasons, this might still be null.
            // TODO: useful stuff.
        }
    }