When I ask the spring application context to give me the main class of my application, using applicationContext.getBeansWithAnnotation(...)
, I can only access two of the class annotations :
@SpringBootApplication
@EnableJpaRepositories
So if I ask for the bean with the annotation @EntityScan
, the spring context correctly give me the main class, but I can not access the @EntityScan
annotation, as if it was not present.
Why ?
Main class
@SpringBootApplication(scanBasePackages = "foo")
@EnableJpaRepositories(basePackages = "foo.repository", repositoryBaseClass = AppRepositoryImpl.class)
@EntityScan(basePackages = "foo.entity") // Spring annotation -> I don't see it
@MiscScan(basePackages = "foo.misc") // My own annotation -> I don't see it
public class APIApplication {
public static void main(String[] args) {
SpringApplication.run(APIApplication.class, args);
}
}
Retrieving the main class from the spring application context
// I can search the main class using any of the 4 aforementioned annotations, it does not matter.
applicationContext.getBeansWithAnnotation(EntityScan.class).values().iterator().next().getClass().getAnnotations()
Note that :
- Changing the order of the annotations does not matter.
APIApplication.class.getAnnotations()
correctly give me all the annotations.
EDIT : The class was proxied by spring, so the get the real class and all the annotations I have to do this :
applicationContext.getBeansWithAnnotation(EntityScan.class).values().iterator().next().getClass().getSuperclass().getAnnotations()
EDIT 2 : I found a cleaner solution :
org.springframework.util.ClassUtils.getUserClass(applicationContext.getBeansWithAnnotation(EntityScan.class).values().iterator().next().getClass()).getAnnotations()