I would like to enable application features based on the presence of a custom annotation that marks the ApplicationConfig.class
as below:
@FooBar(enabled = true)
@Configuration
@ComponentScan(basePackageClasses = Application.class, excludeFilters = @Filter({Controller.class, Configuration.class}))
@EnableJpaRepositories("com.package.repository")
class ApplicationConfig {
// Application specific configs and bean defs.
}
The custom annotation is named FooBar
:
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface FooBar {
boolean enabled() default true;
}
During application startup I would like to detect that this class (or any other class/bean) is annotated with this annotation.
Here is my attempt, partly based on this similar question it includes two ways to determine that the annotation is being used.
@Component
public class MyClassWithEventListeners implements ApplicationContextAware {
@Autowired
ApplicationEventPublisher applicationEventPublisher;
@Autowired
ApplicationContext applicationContext;
@EventListener
void contextRefreshedEvent(ContextRefreshedEvent event) {
ApplicationContext applicationContext = event.getApplicationContext();
applicationContext.getClassLoader();
AutowireCapableBeanFactory autowireCapableBeanFactory = applicationContext.getAutowireCapableBeanFactory();
String[] names = event.getApplicationContext().getBeanDefinitionNames();
for (String name : names) {
Object o = autowireCapableBeanFactory.getBean(name);
if (AopProxyUtils.ultimateTargetClass(o).isAnnotationPresent(FooBar.class)) {
System.out.println("Found class annotated with FooBar");
}
}
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
List<String> beanNames = getBeansWithAnnotation(FooBar.class);
if(beanNames !=null){
System.out.println("Found class annotated with FooBar");
}
}
public List<String> getBeansWithAnnotation(Class<? extends Annotation> type) {
Predicate<Map<String, Object>> filter = Predicates.alwaysTrue();
return getBeansWithAnnotation(type, filter);
}
public List<String> getBeansWithAnnotation(Class<? extends Annotation> type, Predicate<Map<String, Object>> attributeFilter) {
List<String> result = Lists.newArrayList();
ConfigurableListableBeanFactory factory = ((ConfigurableApplicationContext) applicationContext).getBeanFactory();
for (String name : factory.getBeanDefinitionNames()) {
BeanDefinition bd = factory.getBeanDefinition(name);
if (bd.getSource() instanceof StandardMethodMetadata) {
StandardMethodMetadata metadata = (StandardMethodMetadata) bd.getSource();
Map<String, Object> attributes = metadata.getAnnotationAttributes(type.getName());
if (null == attributes) {
continue;
}
if (attributeFilter.apply(attributes)) {
result.add(name);
}
}
}
return result;
}
}
Both the contextRefreshedEvent()
and setApplicationContext()
methods are called and neither are able to detect the custom annotation.
What I have observed is that my ApplicationConfig.class
is present in the list of beans/classes but appears as follows:
com.package.config.ApplicationConfig$$EnhancerBySpringCGLIB$$15073fb3@196887
- What is this
EnhancedBySpring
? - How does Spring add functionality? The
@EnableJpaRepositories
adds repositories. I would like to replicate this functionality for my own purposes.