springboot解析复杂配置

本文详细介绍了一种在SpringBoot项目中实现复杂配置加载的方法,包括动态扩展配置和使用自定义配置类。通过实例展示了如何定义和加载带有多个层级的配置,并提供了具体的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在springboot项目开发过程中经常需要定义一些复杂配置,且该配置还可以动态扩展,现在介绍一种复杂配置加载方式,如下面的配置:

system:
  scenes: aaaa,bbbb
  aaaa:
    name: "场景1"
    sql: "select * from tableA"
    keys:
      - "11:22"
      - "11:22"
  bbbb:
    name: "场景2"
    sql: "select * from tableB"
    keys:
      - "device:device"
      - "mba:mba"

如果配置是这样,则可以定义一个key的字符串列表

import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description:
 */
@Slf4j
@Configuration
@RefreshScope
@Data
public class SceneConfig implements EnvironmentAware {
    @Value("${system.scenes}")
    private String scenesPrefixs;

    private ConcurrentHashMap<String,SceneProperties> sceneConfig=new ConcurrentHashMap<>();

    public SceneProperties getSceneProperties(String scene){
        try{
            return sceneConfig.get(scene).clone();
        }catch (Exception e){
            return null;
        }
    }
    @Override
    @RefreshScope
    public void setEnvironment(Environment environment) {
        initSceneConfig(environment);
    }

    private void initSceneConfig(Environment env) {
        log.info("scenesPrefixs:{}",scenesPrefixs);
        Binder binder = Binder.get(env); //绑定简单配置
        for (String scPrefix : scenesPrefixs.split(",")) {
            SceneProperties param=binder.bind("system." + scPrefix.toLowerCase(), Bindable.of(SceneProperties.class)).get();
            param.setBussinessId(scPrefix);
            sceneConfig.put(scPrefix,param);
        }
    }
}
### Spring Boot 加载解析配置文件的原理 Spring Boot 应用程序在启动期间会自动查找并加载多个可能存在的配置文件,这些文件可以位于不同的位置,并遵循特定的优先级顺序。当应用程序启动时,`SpringApplication.run()` 方法触发了一系列初始化操作,其中包括发布 `ApplicationEnvironmentPreparedEvent` 事件[^1]。 此事件被各种监听器捕获,特别是那些负责处理外部化配置的部分。其中最核心的是 `ConfigFileApplicationListener` 类,该类实现了对不同类型的属性源的支持,比如 `.properties`, `.yml` 或者其他格式的配置文件。一旦检测到此类资源的存在,便会将其内容转换成键值对形式存储于内存之中以便后续访问使用[^3]。 对于默认路径下的配置文件(如 `application.properties`),其搜索范围涵盖了以下几个主要目录: - 当前项目的根目录下 (`./config/`) - 当前项目根目录 - classpath 的 `/config` 子目录内 - classpath 根部 按照上述次序依次尝试读取同名文件;如果存在相同名称但在更靠后的路径,则后者将会覆盖前者中的设置项。此外,还可以利用命令行参数、环境变量等方式动态指定额外的配置文件来满足灵活部署的需求。 为了支持更加复杂的场景需求,开发者也可以编写自己的逻辑来自定义加载过程。这通常涉及到创建一个新的 `PropertySource` 并注册给当前的应用上下文。例如,在某些情况下可以通过实现 `ApplicationContextInitializer` 接口或者继承 `AbstractContextLoaderInitializer` 来达到目的[^2]。 ```java // 自定义 PropertySourcesPlaceholderConfigurer 示例代码 @Configuration public class CustomConfiguration { @Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { final PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer(); Resource[] resources = new ClassPathResource[]{new ClassPathResource("custom-config.properties")}; configurer.setLocations(resources); return configurer; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值