spring配置文件解析

使用spring配置文件解析的两种方式

  • 在bean文件中增加PropertyResourceConfigurer的子类,通常用PropertyPlaceholderConfigurer
  
  
    
      classpath:conf/*.properties
    
  

  • 在bean文件中用context标签

多个配置文件用逗号隔开,这里面有几个默认属性:
ignore-resource-not-found="false"不忽略文件找不到的错误
ignore-unresolvable="false"不忽略无法解析的配置错误
system-properties-mode="ENVIRONMENT"这个默认配置会让spring添加的默认配置文件解析类为PropertySourcesPlaceholderConfigurer,它默认的system-properties-mode为ENVIRONMENT,而不是PropertyPlaceholderConfigurer类的FALLBACK,会优先使用环境变量,再使用配置文件的值,部分代码如下:

if (element.getAttribute(SYSTEM_PROPERTIES_MODE_ATTRIB).equals(SYSTEM_PROPERTIES_MODE_DEFAULT)) {
    return PropertySourcesPlaceholderConfigurer.class;
}
return PropertyPlaceholderConfigurer.class;

PropertyPlaceholderConfigurer解析

从类图中可以看到,其父类PropertyResourceConfigurer实现了BeanFactoryPostProcessor接口,bean factory在初始化的时候,会触发它的postProcessBeanFactory方法。其在完成了配置读取合并,转换成Properties,然后调用子类提供的processProperties方法。


spring配置文件解析_第1张图片
PropertyResourceConfigure.jpg

PropertyPlaceholderConfigurer类的processProperties中,将父类处理好的properties对象包装成PlaceholderResolvingStringValueResolver,然后调用BeanDefinitionVisitor的visitBeanDefinition方法去替换bean的各个部分的占位符。

总结与延伸

PropertyResourceConfigure类及其子类,在完成了配置文件的解析并合并到Properties成员之后,都没有对外提供该properties成员的访问接口,也未对外暴露解析占位符的成员StringValueResolver。为了提供给开发者解析占位符的能力,在spring3.0之后,doProcessProperties方法调用了bean factory的addEmbeddedValueResolver将valueResolver对象添加到了bean factory中,之后可以调用resolveEmbeddedValue方法解析占位符。
通过本篇文章可以了解到,spring解析配置文件是通过PropertyResouceConfigurer类实现了BeanFactoryPostProcess,在bean factory初始化的时候,调用它的方法来实现配置文件初始化的,虽然该类实现是PriorityOrder接口,比实现了Order或者只实现了BeanFactoryPostProcess的接口触发时机要早。但是BeanDefinitionRegistryPostProcessor的触发时机是比BeanFactoryPostProcess要早的,如果你在这个时候想解析占位符,是无法直接通过bean factory提供的resolveEmbeddedValue去解析的,而且PropertyResouceConfigurer类也没有暴露方法给你手动解析占位符。那么如果我们想在这个时机解析占位符要怎么弄呢?可以参考下mybatis的做法:

Map prcs = applicationContext.getBeansOfType(PropertyResourceConfigurer.class);
if (!prcs.isEmpty() && applicationContext instanceof GenericApplicationContext) {
  BeanDefinition mapperScannerBean = ((GenericApplicationContext) applicationContext)
      .getBeanFactory().getBeanDefinition(beanName);
  DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
  factory.registerBeanDefinition(beanName, mapperScannerBean);
  for (PropertyResourceConfigurer prc : prcs.values()) {
    prc.postProcessBeanFactory(factory);
  }
  PropertyValues values = mapperScannerBean.getPropertyValues();
  this.basePackage = updatePropertyValue("basePackage", values);
  this.sqlSessionFactoryBeanName = updatePropertyValue("sqlSessionFactoryBeanName", values);
  this.sqlSessionTemplateBeanName = updatePropertyValue("sqlSessionTemplateBeanName", values);
}


private String updatePropertyValue(String propertyName, PropertyValues values) {
  PropertyValue property = values.getPropertyValue(propertyName);
  if (property == null) {
    return null;
  }
  Object value = property.getValue();
  if (value == null) {
    return null;
  } else if (value instanceof String) {
    return value.toString();
  } else if (value instanceof TypedStringValue) {
    return ((TypedStringValue) value).getValue();
  } else {
    return null;
  }
}

mybatis想解析MapperScannerConfigurer类用户配置的占位符,是取到PropertyResourceConfigurer对象,然后new一个DefaultListableBeanFactory,把需要解析占位符的类添加到factory中,然后模拟factory初始化流程,手动触发postProcessBeanFactory,给目标类解析占位符。

你可能感兴趣的:(spring配置文件解析)