spring profile 多环境配置管理详解
作者:lqh 时间:2023-01-23 17:53:58
spring profile 多环境配置管理
现象
如果在开发时进行一些数据库测试,希望链接到一个测试的数据库,以避免对开发数据库的影响。
开发时的某些配置比如log4j日志的级别,和生产环境又有所区别。
各种此类的需求,让我希望有一个简单的切换开发环境的好办法。
解决
现在spring3.1也给我们带来了profile,可以方便快速的切换环境。
使用也是非常方便。只要在applicationContext.xml中添加下边的内容,就可以了
<!-- 开发环境配置文件 -->
<beans profile="test">
<context:property-placeholder location="/WEB-INF/test-orm.properties" />
</beans>
<!-- 本地环境配置文件 -->
<beans profile="local">
<context:property-placeholder location="/WEB-INF/local-orm.properties" />
</beans>
profile的定义一定要在文档的最下边,否则会有异常。整个xml的结构大概是这样
<beans xmlns="..." ...>
<bean id="dataSource" ... />
<bean ... />
<beans profile="...">
<bean ...>
</beans>
</beans>
激活 profile
spring 为我们提供了大量的激活 profile 的方法,可以通过代码来激活,也可以通过系统环境变量、JVM参数、servlet上下文参数来定义 spring.profiles.active 参数激活 profile,这里我们通过定义 JVM 参数实现。
1、ENV方式:
ConfigurableEnvironment.setActiveProfiles("test")
2、JVM参数方式:
tomcat 中 catalina.bat(.sh中不用“set”) 添加JAVA_OPS。通过设置active选择不同配置文件
set JAVA_OPTS="-Dspring.profiles.active=test"
eclipse 中启动tomcat。项目右键 run as –> run configuration–>Arguments–> VM arguments中添加。local配置文件不必上传Git追踪管理
-Dspring.profiles.active="local"
3、web.xml方式:
<init-param>
<param-name>spring.profiles.active</param-name>
<param-value>production</param-value>
</init-param>
4、标注方式(junit单元测试非常实用):
@ActiveProfiles({"unittest","productprofile"})
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
来源:http://blog.csdn.net/jenny8080/article/details/53185178
标签:spring,profile,环境配置
0
投稿
猜你喜欢
java解析Excel的方法(xls、xlsx两种格式)
2021-10-12 16:29:39
Java网络编程之基于TCP协议
2023-08-20 14:05:51
SpringBoot打包发布到linux上(centos 7)的步骤
2023-08-11 06:35:55
C++实现LeetCode(2.两个数字相加)
2023-06-23 16:51:11
Android提高之BLE开发Android手机搜索iBeacon基站
2023-05-19 18:58:15
Spring Boot中利用JavaMailSender发送邮件的方法示例(附源码)
2023-05-13 02:13:08
深入浅出重构Mybatis与Spring集成的SqlSessionFactoryBean(上)
2021-12-01 18:27:49
Android 使用 SharedPreferences 保存少量数据的实现代码
2023-07-03 01:00:11
为SpringBoot服务添加HTTPS证书的方法
2023-10-11 03:03:22
utf8编码检测方法分享
2023-05-18 12:34:27
基于JAVA中Jersey处理Http协议中的Multipart的详解
2021-06-15 13:43:57
Mybatis模糊查询及自动映射实现详解
2021-10-29 12:05:40
C# 在PDF中添加墨迹注释Ink Annotation的步骤详解
2022-01-31 14:23:29
Android开发之ViewPager实现滑动切换页面
2023-03-10 05:32:43
SpringBoot @NotBlank错误的解决方案
2023-01-12 20:05:59
Java 注解学习笔记
2022-12-25 02:40:54
Java中构造函数,set/get方法和toString方法使用及注意说明
2021-07-15 13:01:39
Android中的Bitmap缓存池使用详解
2023-02-23 15:01:15
Android自定义渐变式炫酷ListView下拉刷新动画
2021-12-26 17:31:09
jpa EntityManager 复杂查询实例
2023-08-31 01:03:01