public ApplicationContext loadContext(String... locations) {
if (logger.isDebugEnabled()) {
logger.debug("Creating a JavaConfigApplicationContext for {}", Arrays.asList(locations));
}
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
ArrayList<Class<?>> configClasses = new ArrayList<Class<?>>();
ArrayList<String> basePackages = new ArrayList<String>();
for (String location : locations) {
// if the location refers to a class, use it. Otherwise assume it's a base package name
try {
final Class<?> aClass = this.getClass().getClassLoader().loadClass(location);
configClasses.add(aClass);
} catch (ClassNotFoundException e) {
if (Package.getPackage(location) == null) {
throw new IllegalArgumentException(
String.format("A non-existent class or package name was specified: [%s]", location));
}
basePackages.add(location);
}
}
logger.debug("Setting config classes to {}", configClasses);
logger.debug("Setting base packages to {}", basePackages);
for (Class<?> configClass : configClasses) {
context.register(configClass);
}
for (String basePackage : basePackages) {
context.scan(basePackage);
}
context.refresh();
// Have to create a child context that implements BeanDefinitionRegistry
// to pass to registerAnnotationConfigProcessors, since
// JavaConfigApplicationContext does not
final GenericApplicationContext gac = new GenericApplicationContext(context);
AnnotationConfigUtils.registerAnnotationConfigProcessors(gac);
// copy BeanPostProcessors to the child context
for (String bppName : context.getBeanFactory().getBeanNamesForType(BeanPostProcessor.class)) {
gac.registerBeanDefinition(bppName, context.getBeanFactory().getBeanDefinition(bppName));
}
gac.refresh();
gac.registerShutdownHook();
return gac;