一、结构类图

 spring源码解析(一)---占位符解析替换

①、PropertyResolver : Environment的顶层接口,主要提供属性检索和解析带占位符的文本。bean.xml配置中的所有占位符例如${}都由它解析

②、ConfigurablePropertyResolver : 该接口定义了如何对组件本身进行配置。如:刚刚提到获取value时可以指定任意类型,这依赖于ConversionService进行类型转换,当前接口就提供了对ConversionService的设置和获取。另外,可以配置属性占位符的格式,包括:占位符前缀(默认为"${")、占位符后缀(默认为"}")、占位符值分隔符(默认为":",用于分隔propertyName和defaultValue)。组件还可以设置哪些属性是必须存在的,还可以校验必须存在的属性是否真的存在(不存在的话会抛出异常)

③、AbstractPropertyResolver : 实现了ConfigurablePropertyResolver接口的所有方法

④、PropertySourcesPropertyResolver : 以PropertySources属性源集合(内部持有属性源列表List<PropertySource>)为属性值的来源,按序遍历每个PropertySource,获取到一个非null的属性值则返回

二、demo示例

public static void main(String[] args) {
        Properties properties = System.getProperties();
        
        properties.setProperty("prefixName", "read-code");
        
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:${prefixName}-spring.xml");
        
        ReadCodeService readCodeService = (ReadCodeService) ac.getBean("readCodeService");
        
        readCodeService.say();
    }
View Code

相关文章:

  • 2021-07-14
  • 2022-12-23
  • 2021-12-17
  • 2022-01-01
  • 2022-12-23
  • 2021-09-22
  • 2022-01-09
  • 2021-10-11
猜你喜欢
  • 2022-02-08
  • 2021-07-01
  • 2022-01-14
  • 2021-08-15
  • 2021-10-31
  • 2022-12-23
  • 2021-12-06
相关资源
相似解决方案