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

文章讨论了将Springboot项目中的自定义工具类扩展应用到Spring框架时,关于`Environment`和`context:placeholder`配置获取的差异。Springboot能从`application.yml`的Environment获取配置,而Spring的`PropertySourcesPropertyResolver`则不能。原因在于Springboot将配置直接注入Environment,而Spring需要手动处理配置加载过程。
摘要由CSDN通过智能技术生成

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中读取的问题。

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值