How to list Spring java configuration classes?

I am using Java Spring based configuration in my application. I have a root configuration class that imports many other configurations, each of which can import more configurations, etc .:

@Config @Import(AnotherConfig.class) class RootConfig { // beans } @Config @Import(YetAnotherConfig.class) class AnotherConfig { // beans } @Config class YetAnotherConfig { // beans } 

Then I boot the system by declaring AnnotationConfigWebApplicationContext in my web.xml file for which contextConfigLocation set to RootConfig .

Is there a way to define the full set of configuration classes that loaded my application? Some functions like this (may have a subclass for ctx ...):

 List<Class> readConfigClasses(ApplicationContext ctx) { // what goes here? // should return [ RootConfig.class, // AnotherConfig.class, // YetAnotherConfig.class ] } 

Update : @axtavt's answer gets most of the way, returning the objects of the configuration itself, which in my case are instances extended by CGLIB. Capturing the superclass of these proxies does the trick:

 List<Class<?>> readConfigClasses(final ApplicationContext ctx) { List<Class<?>> configClasses = new ArrayList<Class<?>>(); for (final Object config : ctx.getBeansWithAnnotation(Configuration.class).values()) { configClasses.add(config.getClass().getSuperclass()); } return configClasses; } 
+4
source share
1 answer
Classes

@Configuration managed using Spring as regular beans, so you can list them as ctx.getBeansWithAnnotation(Configuration.class) .

However, I'm not sure how it will work in complex cases ( @Bean in @Configuration classes, etc.).

+1
source

Source: https://habr.com/ru/post/1501470/


All Articles