通过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);
    }
}
相关推荐
默 语5 小时前
基于 Spring Boot 3 + LangChain4j 快速构建企业级 AI 应用实战
人工智能·spring boot·后端
薪火铺子5 小时前
SpringBoot WebServer启动与监听器原理深度解析
spring boot·后端·tomcat
KmSH8umpK6 小时前
SpringBoot 分布式锁实战:从单机锁到Redis分布式锁全覆盖,解决超卖、重复下单、幂等并发问题
spring boot·redis·分布式
jay神6 小时前
基于团队模式的C程序设计课程辅助教学管理系统
java·spring boot·vue·web开发·管理系统
长河7 小时前
基于 Jib 实现无 Dockerfile 的 Spring Boot 应用容器化
java·spring boot·后端
Arya_aa8 小时前
一:病虫害 AI 识别系统项目初期准备与Docker初识,VM虚拟机
spring boot
敖正炀8 小时前
Spring MVC 启动全景:DispatcherServlet 与父子容器
spring boot
绿草在线10 小时前
基于SpringBoot4+Mybatis+Thymeleaf的用户管理系统开发实战
java·spring boot·thymeleaf
麦麦大数据10 小时前
基于以太坊区块链+Spring Boot+Solidity智能合约的投票系统设计与实现
spring boot·后端·区块链·智能合约·投票系统