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 {
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; }
source share