springboot优雅的获取yml配置

简介: springboot优雅的获取yml配置

1. 首先配置Bean

import com.vivo.admin_log.common.utils.YamlConfigurerUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import java.util.Properties;
/** * @author 72038611 */
@Configuration
public class BeanConfiguration { 
    private final Logger logger = LoggerFactory.getLogger(BeanConfiguration.class);
    @Bean
    public YamlConfigurerUtil ymlConfigurerUtil() { 
        //1:加载配置文件
        Resource app = new ClassPathResource("config/application.yml");
        Resource appDev = new ClassPathResource("config/application-dev.yml");
        Resource appProd = new ClassPathResource("config/application-prod.yml");
        YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
        // 2:将加载的配置文件交给 YamlPropertiesFactoryBean
        yamlPropertiesFactoryBean.setResources(app);
        // 3:将yml转换成 key:val
        Properties properties = yamlPropertiesFactoryBean.getObject();
        String active = properties.getProperty("spring.profiles.active");
        if (active == "" || active == null) { 
            logger.error("未找到spring.profiles.active配置!");
        }else { 
            //判断当前配置是什么环境
            if ("dev".equals(active)) { 
                yamlPropertiesFactoryBean.setResources(app,appDev);
            }else if("prod".equals(active)){ 
                yamlPropertiesFactoryBean.setResources(app,appProd);
            }
        }
        // 4: 将Properties 通过构造方法交给我们写的工具类
        YamlConfigurerUtil ymlConfigurerUtil = new YamlConfigurerUtil(yamlPropertiesFactoryBean.getObject());
        return ymlConfigurerUtil;
    }
}

2. 工具类

import java.util.Properties;
/** * @author 72038611 */
public class YamlConfigurerUtil { 
    private static Properties ymlProperties = new Properties();
    public YamlConfigurerUtil(Properties properties){ 
        ymlProperties = properties;
    }
    public static String getStrYmlVal(String key){ 
        return ymlProperties.getProperty(key);
    }
    public static Integer getIntegerYmlVal(String key){ 
        return Integer.valueOf(ymlProperties.getProperty(key));
    }
}
相关文章
|
2天前
|
Java 调度 Spring
SpringBoot实现多线程定时任务动态定时任务配置文件配置定时任务
SpringBoot实现多线程定时任务动态定时任务配置文件配置定时任务
287 0
|
2天前
|
SQL Java 数据库连接
springboot中配置mybatis别名该怎么写?
springboot中配置mybatis别名该怎么写?
50 0
|
2天前
|
druid Java 数据库
druid+springboot加解密Druid链接池配置加密密码链接数据库
druid+springboot加解密Druid链接池配置加密密码链接数据库
96 0
|
2天前
|
前端开发 Java 应用服务中间件
Springboot对MVC、tomcat扩展配置
Springboot对MVC、tomcat扩展配置
|
2天前
|
安全 Java 开发者
深入理解Spring Boot配置绑定及其实战应用
【4月更文挑战第10天】本文详细探讨了Spring Boot中配置绑定的核心概念,并结合实战示例,展示了如何在项目中有效地使用这些技术来管理和绑定配置属性。
14 1
|
2天前
|
Java 文件存储 Spring
【springboot】logback配置
【springboot】logback配置
20 1
|
2天前
|
Java 微服务 Spring
Spring Boot中获取配置参数的几种方法
Spring Boot中获取配置参数的几种方法
22 2
|
2天前
|
Web App开发 前端开发 Java
SpringBoot配置HTTPS及开发调试
在实际开发过程中,如果后端需要启用https访问,通常项目启动后配置nginx代理再配置https,前端调用时高版本的chrome还会因为证书未信任导致调用失败,通过摸索整理一套开发调试下的https方案,特此分享
21 0
SpringBoot配置HTTPS及开发调试
|
2天前
|
存储 Java 数据库
SpringBoot使用jasypt实现数据库配置加密
这样,你就成功地使用Jasypt实现了Spring Boot中的数据库配置加密,确保敏感信息在配置文件中以加密形式存储,并在应用启动时自动解密。
47 2
|
2天前
|
Java Shell 测试技术
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
40 0
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析