关闭

Spring factory load

标签: springpropertiesreturningcachingbeanapplication
480人阅读 评论(0) 收藏 举报
分类:

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;

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2347677次
    • 积分:31434
    • 等级:
    • 排名:第156名
    • 原创:962篇
    • 转载:826篇
    • 译文:6篇
    • 评论:39条
    文章分类
    最新评论
    我的栏目
    我的栏目测试