springboot优雅的获取yml配置(区分生产环境和测试环境)
2019-04-30 17:25
489 查看
#首先配置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; } }
#工具类
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)); } }
改编自https://blog.csdn.net/xiao______xin/article/details/86700939#comments
相关文章推荐
- Spring Boot之分离测试和生产环境的应用配置
- springboot 多环境(开发、测试、生产)部署配置文件方案
- Spring Boot 便捷修改生产环境和测试环境下的配置文件
- SpringBoot yml 配置 多配置文件,开发环境,生产环境配置文件分开
- Spring Boot之分离测试和生产环境的应用配置
- 通过 spring 容器内建的 profile 功能实现开发环境、测试环境、生产环境配置自动切换
- 通过 spring 容器内建的 profile 功能实现开发环境、测试环境、生产环境配置自动切换
- SpringBoot学习:获取yml和properties配置文件的内容
- Spring boot 使用YML文件配置多环境
- springboot下配置junit测试环境
- springboot yml配置文件多环境配置
- shell 管理SpringBoot 生产环境服务 (配置JVM)
- 通过 spring 容器内建的 profile 功能实现开发环境、测试环境、生产环境配置自动切换
- Spring.profile实现开发、测试和生产环境的配置和切换
- 通过 spring 容器内建的 profile 功能实现开发环境、测试环境、生产环境配置自动切换
- 160922、配置:spring通过profile或@profile配置不同的环境(测试、开发、生产)
- springboot 使用spring.profiles.active 区分不同环境下配置文件
- Spring.profile实现开发、测试和生产环境的配置和切换
- spring boot 配置文件properties,yml语法学习及属性获取@ConfigurationProperties和@Value
- 通过 spring 容器内建的 profile 功能实现开发环境、测试环境、生产环境配置自动切换