spring boot 自定义读取配置文件

Spring Boot支持多种格式配置文件,可通过xml或@ImportResource引入properties或yaml格式。若要拉取远程配置,可山寨阿波罗实现。文中给出小demo,虽仅简单读取本地文件,但可拓展支持git、数据库等,核心在postProcessBeanFactory方法处理数据。

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

在spring boot 中支持很多种格式配置文件,可以通过xml或者@ImportResource 来引入格式只能是 properties(或者 yaml)。若是想拉取远程配置,该如何处理呢?我们可以山寨一下阿波罗的实现,写了个小demo,抛砖引玉,不喜勿喷。

1 山寨第一步自定义注解启用自定义配置,为了方便我只是简单读取某个路径下的文件。

package com.xuyw.annotation;

import com.xuyw.utils.config.MyFilePropertyRegistrar;
import org.springframework.context.annotation.Import;

import java.lang.annotation.*;

/**
 * @author one.xu
 * @version v1.0
 * @description
 * @date 2019/5/5 16:25
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(MyFilePropertyRegistrar.class)
public @interface EnableMyFileProperty {
    String[] value() default {};
}
package com.xuyw.utils.config;

import com.xuyw.annotation.EnableMyFileProperty;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;

import java.util.Objects;

/**
 * @author one.xu
 * @version v1.0
 * @description
 * @date 2019/5/5 16:28
 */
public class MyFilePropertyRegistrar implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry registry) {
        AnnotationAttributes attributes = AnnotationAttributes.fromMap(annotationMetadata
                .getAnnotationAttributes(EnableMyFileProperty.class.getName()));
        String[] path = attributes.getStringArray("value");
        if (path == null || path.length == 0) {
            return;
        }
        if (registry.containsBeanDefinition(PropertySourcesProcessor.class.getName())) {
            return;
        }

        String[] candidates = registry.getBeanDefinitionNames();

        for (String candidate : candidates) {
            BeanDefinition beanDefinition = registry.getBeanDefinition(candidate);
            if (Objects.equals(beanDefinition.getBeanClassName(), PropertySourcesProcessor.class.getName())) {
                return;
            }
        }
        PropertySourcesProcessor.setFilePath(path);
        BeanDefinition annotationProcessor = BeanDefinitionBuilder.genericBeanDefinition(PropertySourcesProcessor.class).getBeanDefinition();
        registry.registerBeanDefinition(PropertySourcesProcessor.class.getName(), annotationProcessor);
    }
}
package com.xuyw.utils.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;

/**
 * @author one.xu
 * @version v1.0
 * @description
 * @date 2019/5/5 16:59
 */
public class PropertySourcesProcessor implements BeanFactoryPostProcessor, EnvironmentAware {
    private static final String MY_PROPERTY_SOURCE_NAME = "MyPropertySources";
    private static String[] FILE_PATH = null;
    private ConfigurableEnvironment environment;

    public static void setFilePath(String[] paths) {
        FILE_PATH = paths;
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        if (environment.getPropertySources().contains(MY_PROPERTY_SOURCE_NAME)) {
            return;
        }
        if (FILE_PATH == null) {
            return;
        }
        //读取文件
        CompositePropertySource composite = new CompositePropertySource(MY_PROPERTY_SOURCE_NAME);
        for (String s : FILE_PATH) {
            composite.addPropertySource(new MyFilePropertySource(MY_PROPERTY_SOURCE_NAME + "_" + s.replace("\\", "_"), file2Map(s)));
        }

        environment.getPropertySources().addFirst(composite);
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = (ConfigurableEnvironment) environment;
    }

    private Map<String, String> file2Map(String file) {
        Map<String, String> resultMap = new HashMap<>();
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
            String s;
            while ((s = br.readLine()) != null) {
                if (StringUtils.isEmpty(s)) {
                    continue;
                }
                String[] configs = s.split("=");
                resultMap.put(StringUtils.trimAllWhitespace(configs[0]), StringUtils.trimAllWhitespace(configs[1]));
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultMap;
    }
}

启动类加上

@EnableMyFileProperty({"C:\\opt\\config\\1.properties"})

由于本人偷懒,为了简单只是读取本地文件。其实完全可以拓展让其支持原创url 例如git 或者数据库中拉取,再自定义数据格式策略实现使其能够支持xml json ,核心就在postProcessBeanFactory 方法拉取数据处理,有兴趣可以自己撸一撸。

Spring Boot 提供了非常便捷的方式来自定义 `application.yml` 配置文件,并将其属性绑定到 Java 对象中,这不仅提高了代码的灵活性还增强了项目的可维护性。 ### 一、自定义配置项 假设你需要添加一个新的数据库连接池的相关设置,可以在原有的 `application.yml` 中直接追加相应的键值对: ```yaml spring: datasource: hikari: minimum-idle: 5 maximum-pool-size: 20 ``` 这里我们只是简单地调整 HikariCP 连接池的一些默认参数。当然也可以创建完全独立于 Spring 的新组配置信息,例如为应用指定一些业务规则或外部服务API密钥等个性化内容: ```yaml myapp: api-key: "your_secret_api_key" features: enabled-feature-a: true beta-mode: false ``` ### 二、读取配置至POJO 为了将上述自定义配置映射成易于操作的对象实例,在项目里新建一个 Java 类并利用注解 @ConfigurationProperties 或者更推荐使用新版中的 @Value 注入单个值;如果是一个完整的配置块则适合前者。 #### 使用@ConfigurationProperties方式 首先需要开启@EnableConfigurationProperties支持(通常已经在主应用程序类上存在),然后定义一个对应的实体bean: ```java import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component //让这个组件能被自动装配进IOC容器 @ConfigurationProperties(prefix = "myapp")//前缀对应yml里的节点名称 public class MyAppConfig { private String apiKey; private Features features; public static class Features { boolean enabledFeatureA; boolean betaMode; getters/setters... } getters/setters... } ``` 接着就可以通过依赖注入轻松获取这些配置数据啦! 另外需要注意的是当修改完配置之后记得重启程序使之生效哦~不过对于部分场景下动态刷新的需求,可以结合Actuator端点完成即时更新功能~
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值