spring profile激活处理
来源:互联网 发布:20世纪90年代网络繁荣 编辑:程序博客网 时间:2024/05/29 15:23
1.使用背景
项目开发一共有三个环境:测试环境,灰度环境和生产环境,比如我们想在测试环境下,不加载某些配置信息,可以通过profile来实现
2.激活profile实现方式
- JVM增加参数spring.profiles.active设置
- 在ServletContextListener 中初始化属性spring.profiles.active
3. JVM增加参数spring.profiles.active设置
在JVM中增加参数spring.profiles.active设置,如果我们想设置spring.profiles.active为dev,使用Dspring.profiles.active=”dev” 。
此种方式需要修改tomcat的JVM配置,通用性不高。
4. 在ServletContextListener 中初始化spring.profiles.active
写一个类InitConfigListener实现接口ServletContextListener,重写容器初始化方法contextInitialized(),设置属性为spring.profiles.active为指定值environment。
environment可以定义在一个属性文件中,在使用maven构建时使用测试,灰度或者生产环境的属性文件。
在contextInitialized方法中读取指定属性文件,获取environment 值,通过setProperty即可实现。
@WebListenerpublic class InitConfigListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { String environment = ""; //加载Properties属性文件获取environment值 //侦测jvm环境,并缓存到全局变量中 String env = System.setProperty("spring.profiles.active",environment); } @Override public void contextDestroyed(ServletContextEvent sce) { }}
spring.xml配置只在dev模式下加载配置文件spring-mybatis.xml
<beans profile="dev"> <import resource="spring-mybatis.xml" /> </beans>
springboot使用注解@Profile和@Configuration来配置,@ActiveProfiles()在测试时切换环境
大家可以关注我的公众号:不知风在何处,相互沟通,共同进步。
阅读全文
0 0
- spring profile激活处理
- spring-激活profile(java)
- Spring @Profile总是无法激活的解决办法
- 开发框架-Spring-Profile的配置、激活和读取
- Spring profile
- spring profile
- spring profile
- 激活Maven profile同时打包
- Maven profile整合Spring profile
- Maven profile整合Spring profile
- spring学习--spring profile
- 激活Maven profile的几种方式
- 激活maven profile的四种方式
- 激活Maven profile的几种方式
- 激活Maven profile的几种方式
- 激活maven特定参数profile的方法
- maven profile的不同激活方法
- 激活Maven profile的几种方式
- docker容器间网络通信
- 学习笔记:复习单调队列
- linux下的select简直太奇葩了:1024限定的不只是监听的个数,还是文件描述符的最大值,注意,是值
- Java经典算法40例(一)
- 匿名内部类
- spring profile激活处理
- Docker 安装配置 in Ubuntu
- 关于Html模板中文显示不了问题
- 栈线性表的实现方式 seqstack.h 和 seqstack.c
- Nginx学习笔记(一)
- 教堂见闻
- 自己写一个html!【2017.7.9】
- 20170709_简述ARP的工作原理
- hybrid- Cordova环境安装配置