Detect that a class is marked by a custom annotation in Spring
I'm not that familiar with Spring, but if your goal is to get all types annotated with FooBar and then the enabled attribute, you may just use the Reflections library.
When your application is bootstrapping you would:

Reflections reflections = new Reflections("my.project");


And in your listener:

@EventListener
void contextRefreshedEvent(ContextRefreshedEvent event) throws NoSuchFieldException, IllegalAccessException {
    Set&lt;Class&lt;?&gt;&gt; classes = reflections.getTypesAnnotatedWith(FooBar.class);
    for (Class clazz : classes) {
        boolean enabled = clazz.getAnnotation(FooBar.class).enabled();
        System.out.println("Enabled: " + enabled);
    }
}


Notice that you don't need to get all annotations in the class, check types and then cast if you use getAnnotation. The advantage of using Reflections is that class metadata is indexed, which means the expensive reflection operations aren't done all the time. It won't work, however, if Spring changes annotations on runtime (which I don't know if it does or not). If you can't use the Reflections library, just the getAnnotation method is already an improvement over your original for over each annotation.