if (!classpathPrefix.endsWith("/"))
classpathPrefix = classpathPrefix + "/"; GenericApplicationContext appContext = null;
if (useCache)
appContext = getCachedContext(classpathPrefix, batchName);
if (appContext != null) {
log.debug("Returning cached application context:" + appContext);
return appContext;
} // create and load new instance
appContext = new GenericApplicationContext(); log.info("Loading spring config for environment: " + batchName);
// Get the bean definitions for the environment
ClassPathResource config = new ClassPathResource(classpathPrefix + batchName + CONFIG_SUFFIX);
logResource("spring config", batchName, config);
if (!config.exists())
throw new RuntimeException("Config not found for environment: " + batchName);
XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(appContext);
xmlReader.loadBeanDefinitions(config); // Get any local bean definitions
ClassPathResource localConfig = new ClassPathResource(classpathPrefix + LOCAL_ENV + CONFIG_SUFFIX);
logResource("spring config", LOCAL_ENV, localConfig);
if (localConfig.exists())
xmlReader.loadBeanDefinitions(localConfig); // Get property placeholders
Resource envProps = new ClassPathResource(classpathPrefix + envName + PROPERTIES_SUFFIX);
logResource("spring properties", envName, envProps);
Resource passwordProps = new ClassPathResource(classpathPrefix + envName + PASSWORD_SUFFIX);
logResource("spring properties", envName, passwordProps);
Resource localProps = new ClassPathResource(classpathPrefix + LOCAL_ENV + PROPERTIES_SUFFIX);
logResource("spring properties", LOCAL_ENV, localProps); // Load together to allow overriding in local env
Resource[] ra = combineExistingResources(envProps, passwordProps, localProps);
if (ra.length > 0) {
PropertyPlaceholderConfigurer placeholderConfig = new PropertyPlaceholderConfigurer();
placeholderConfig.setLocations(ra);
placeholderConfig.postProcessBeanFactory(appContext.getDefaultListableBeanFactory());
} // Get property overrides
Resource envOverrideProps = new ClassPathResource(classpathPrefix + envName + OVERRIDES_SUFFIX);
logResource("spring override properties", envName, envOverrideProps);
Resource localOverrideProps = new ClassPathResource(classpathPrefix + LOCAL_ENV + OVERRIDES_SUFFIX);
logResource("spring override properties", LOCAL_ENV, localOverrideProps); // load together to allow overriding in local env
Resource[] rao = combineExistingResources(envOverrideProps, localOverrideProps);
if (rao.length > 0) {
PropertyOverrideConfigurer overrideConfig = new PropertyOverrideConfigurer();
overrideConfig.setLocations(rao);
overrideConfig.postProcessBeanFactory(appContext.getDefaultListableBeanFactory());
} // Cache before refreshing to allow a certain amount of self-referencing in the beans
log.info("Caching app context:" + appContext);
cacheContext(classpathPrefix, batchName, appContext);
log.info("Refreshing app context:" + appContext);
appContext.refresh();
log.info("Returning app context:" + appContext);
return appContext;
标签:load,logResource,Spring,appContext,factory,classpathPrefix,spring,ClassPathResou
From: https://blog.51cto.com/u_16174476/6604468