Spring与SpringBoot在配置读取方式上的区别

1. 问题说明

将Springboot项目中自定义的一个扩展工具类移植到Spring框架项目中的时候发现一个问题。在springboot中application.yml中的配置内容可以从Environment中获取,但是在spring中context:placeholder对应的配置文件中的配置却无法从Environment中获取。为了搞清楚这一问题,对该部分源码进行深入阅读。

2. Spring中placeholder

2.1 核心源码

2.2 代码示例

import org.junit.jupiter.api.Test;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySourcesPropertyResolver;

import java.util.Properties;

/**
 * @author pp_lan
 */
public class PropertiesPropertySourceDemo {

    @Test
    public void test01() {
        Properties properties = new Properties();
        properties.put("dbType", "postgresql");

        PropertiesPropertySource simplePropertySource = new PropertiesPropertySource("simple", properties);
        MutablePropertySources propertySources = new MutablePropertySources();
        propertySources.addLast(simplePropertySource);

        PropertySourcesPropertyResolver resolver = new PropertySourcesPropertyResolver(propertySources);

        String dbType = resolver.resolvePlaceholders("${dbType}");
        System.out.println(dbType);
    }
}
DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Found key 'dbType' in PropertySource 'simple' with value of type String

postgresql

Process finished with exit code 0

3. Springboot中application.yml

3.1 流程图

3.2 源码解析

3.2.1 EnvironmentPostProcessorApplicationListener

3.2.2 加载所有的postProcessor,包括自定义的扩展类

类名

作用

SystemEnvironmentPropertySourceEnvironmentPostProcessor

加载环境变量

ConfigDataEnvironmentPostProcessor

读取配置处理类

3.2.3 读取配置回填入口

org.springframework.boot.context.config.ConfigDataEnvironment

3.2.4 配置读取

org.springframework.boot.context.config.ConfigDataEnvironmentContributor

org.springframework.boot.context.config.StandardConfigDataLoader

StandardConfigDataLoader.load根据resouce的类型选择对应的策略sourceLoader(例如:yml文件选择YamlPropertySourceLoader)

org.springframework.boot.env.YamlPropertySourceLoader

4. 总结

二者主要区别如下:

  • placeholder的方式为变量替换
  • application.yml的读取是将配置内容放入到environment中

因此,导致了Spring无法从Environment中读取的问题。

相关推荐

  1. SpringBoot整理-Spring BootSpring MVC区别

    2024-04-24 12:50:04       61 阅读
  2. springspringboot区别

    2024-04-24 12:50:04       64 阅读
  3. SpringSpringBoot区别

    2024-04-24 12:50:04       61 阅读
  4. springspringboot区别

    2024-04-24 12:50:04       52 阅读
  5. Spring、SpringMVC、SpringBoot区别

    2024-04-24 12:50:04       44 阅读
  6. springspringboot区别

    2024-04-24 12:50:04       38 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-24 12:50:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-24 12:50:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-24 12:50:04       82 阅读
  4. Python语言-面向对象

    2024-04-24 12:50:04       91 阅读

热门阅读

  1. 项目开发的详细步骤(精华版)

    2024-04-24 12:50:04       34 阅读
  2. FlinkSQL State的生命周期

    2024-04-24 12:50:04       33 阅读
  3. Android Binder——数据传递载体(二十一)

    2024-04-24 12:50:04       36 阅读
  4. 定时备份mysql数据库

    2024-04-24 12:50:04       35 阅读
  5. 100.qt qml-MultiPointTouchArea多点触摸缩放拖拽

    2024-04-24 12:50:04       29 阅读
  6. Mysql 8.0 的一些坑

    2024-04-24 12:50:04       37 阅读
  7. Eureka详解

    2024-04-24 12:50:04       30 阅读
  8. css-深度选择器-vue2

    2024-04-24 12:50:04       36 阅读
  9. 整理Meta GDC 2024 上关于XR、空间计算相关的分享

    2024-04-24 12:50:04       34 阅读
  10. 软件步骤2:OpenMVG特征提取(基于SFM场景)

    2024-04-24 12:50:04       25 阅读