通过YamlPropertiesFactoryBean构建配置文件读取工具类

可以看一下介绍,这里只提供实现代码。---> 多种yml/properties配置文件读取方式

以下正文:

1.自定义YamlConfigTool工具类

java 复制代码
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

public class YamlConfigTool {

    private static Properties ymlProperties = new Properties();

    public static Properties getYmlProperties() {
        return ymlProperties;
    }

    public static void setYmlProperties(Properties ymlProperties) {
        YamlConfigTool.ymlProperties = ymlProperties;
    }

    public static YamlConfigTool create(Properties properties) {
        return new YamlConfigTool(properties);
    }

    /**
     * 有参构造
     * @param properties
     */
    public YamlConfigTool(Properties properties) {
        setYmlProperties(properties);
    }

    public static String getStr(String key) {
        return ymlProperties.getProperty(key);
    }

    public static Integer getInt(String key) {
        return Integer.valueOf(ymlProperties.getProperty(key));
    }

    public static Boolean getBoo(String key) {
        return Boolean.valueOf(ymlProperties.getProperty(key));
    }

    public static List<String> getList(String key) {
        return Arrays.asList(ymlProperties.getProperty(key).split(","));
    }

2.定义公共的读取内容

java 复制代码
import org.springframework.stereotype.Component;

/**
 * @author 多个接口以区分对应的配置
 * @date 2020-03-04 11:02
 */
@Component
public class CommonTool {

    /**
     * 可以多个接口,也可以多个类,用于区分
     */
    public interface RedieEnum{
      
        String MANAGER_OFFICE = YamlConfigTool.getStr("AAAAA.BBBB");

    }


    //其他需要通过统一工具返回的内容
    //public static Db getDb(String type) {
    //    return Db.use(DSFactory.get(type));
    //}
    //
    //public static Session getSession(String type) {
    //    return Session.create(DSFactory.get(type));
    //}
}

3.重写YamlPropertiesFactoryBean(重要一步)

java 复制代码
   /**
     * 构建配置文件读取操作统一工具类
     */
    @Value("${spring.profiles.active}")
    private String profilesActive;

    @Bean
    public YamlConfigTool ymlConfigurerUtil() {
        //1:加载配置文件
        YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
        // 2:将加载的配置文件交给 YamlPropertiesFactoryBean
        yamlPropertiesFactoryBean.setResources(new ClassPathResource("application.yml"), new ClassPathResource(StrUtil.format("application-{}.yml", profilesActive)));
        // 3:将yml转换成 key:val
        Properties properties = yamlPropertiesFactoryBean.getObject();
        // 4: 将Properties 通过构造方法交给我们写的工具类
        return new YamlConfigTool(properties);
    }

4.测试

java 复制代码
@SpringBootTest
public class TestSomething {
    @Test
    void contextLoads() {
        System.err.println(CommonTool.RedieEnum.MANAGER_OFFICE);
    }
}
相关推荐
zl97989918 小时前
SpringBoot-Web开发之数据响应
java·spring boot·后端
也许是_18 小时前
Spring Boot 3.X推荐Micrometer Tracing 分布式链路追踪
spring boot·分布式·后端
没有bug.的程序员19 小时前
Spring Boot 起步:自动装配的魔法
java·开发语言·spring boot·后端·spring·1024程序员节
兜兜风d'21 小时前
RabbitMQ 高级特性:消息确认机制详解
spring boot·分布式·rabbitmq·java-rabbitmq·1024程序员节
lang2015092821 小时前
Spring Boot健康检查全解析
java·spring boot·后端
来一杯龙舌兰1 天前
【Sentinel】Springboot整合Sentinel、Socket进行熔断限流(生产级熔断限流)
spring boot·后端·sentinel·熔断限流
在等晚安么1 天前
记录自己写项目的第三天,springbot+redis+rabbitma高并发项目
java·spring boot·redis·1024程序员节
Rocket MAN1 天前
Spring Boot 缓存知识体系大纲
spring boot·spring
Knight_AL1 天前
Spring Boot 实现 DOCX 转 PDF(基于 docx4j 的轻量级开源方案)
spring boot·pdf·开源