public ApplicationContext loadContext(String... locations) {
if (logger.isDebugEnabled()) {
logger.debug("Creating a JavaConfigApplicationContext for {}", Arrays.asList(locations));
}
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
List<Class<?>> configClasses = new ArrayList<Class<?>>();
List<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();
return context;
}