通过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);
    }
}
相关推荐
congvee22 分钟前
springboot 学习第1期 - 创建工程
spring boot
风流 少年1 小时前
Cursor创建Spring Boot项目
java·spring boot·后端
毕设源码_钟学姐1 小时前
计算机毕业设计springboot宿舍管理信息系统 基于Spring Boot的高校宿舍管理平台设计与实现 Spring Boot框架下的宿舍管理系统开发
spring boot·后端·课程设计
军军君012 小时前
基于Springboot+UniApp+Ai实现模拟面试小工具二:后端项目搭建
前端·javascript·spring boot·spring·微信小程序·前端框架·集成学习
全栈凯哥2 小时前
16.Spring Boot 国际化完全指南
java·spring boot·后端
后端小肥肠5 小时前
效率革命!10分钟用Dify+Spring Boot打造AI热点雷达,自媒体选赛道再不难!(附保姆级教程)
人工智能·spring boot·agent
Linn5 小时前
Spring WebSocket 服务实现的主流方案与最佳实践
spring boot·后端·spring
我是老孙6 小时前
Spring Boot 应用中,配置的加载优先级
spring boot·后端·pycharm
风象南6 小时前
基于 SpringBoot 的 REST API 与 RPC 调用的统一封装
java·spring boot·后端
weixin_456904278 小时前
Spring Boot整合MyBatis+MySQL+Redis单表CRUD教程
spring boot·mysql·mybatis