通过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);
    }
}
相关推荐
考虑考虑10 小时前
Jpa使用union all
java·spring boot·后端
阿杆20 小时前
同事嫌参数校验太丑,我直接掏出了更优雅的 SpEL Validator
java·spring boot·后端
昵称为空C1 天前
SpringBoot3 http接口调用新方式RestClient + @HttpExchange像使用Feign一样调用
spring boot·后端
麦兜*2 天前
MongoDB Atlas 云数据库实战:从零搭建全球多节点集群
java·数据库·spring boot·mongodb·spring·spring cloud
麦兜*2 天前
MongoDB 在物联网(IoT)中的应用:海量时序数据处理方案
java·数据库·spring boot·物联网·mongodb·spring
汤姆yu2 天前
基于springboot的毕业旅游一站式定制系统
spring boot·后端·旅游
计算机毕业设计木哥2 天前
计算机毕设选题推荐:基于Java+SpringBoot物品租赁管理系统【源码+文档+调试】
java·vue.js·spring boot·mysql·spark·毕业设计·课程设计
hdsoft_huge2 天前
Java & Spring Boot常见异常全解析:原因、危害、处理与防范
java·开发语言·spring boot
AD钙奶-lalala2 天前
SpringBoot实现WebSocket服务端
spring boot·后端·websocket
毕设源码-朱学姐2 天前
【开题答辩全过程】以 4S店汽车维修保养管理系统为例,包含答辩的问题和答案
java·spring boot·汽车