现象
如果在开发时进行一些数据库测试,希望链接到一个测试的数据库,以避免对开发数据库的影响。
开发时的某些配置比如log4j日志的级别,和生产环境又有所区别。
各种此类的需求,让我希望有一个简单的切换开发环境的好办法。
解决
现在spring3.1也给我们带来了profile,可以方便快速的切换环境。
使用也是非常方便。只要在applicationContext.xml中添加下边的内容,就可以了
1
2
3
4
5
6
7
8
9
|
<!-- 开发环境配置文件 -->
< 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的结构大概是这样
1
2
3
4
5
6
7
|
< beans xmlns = "..." ...>
< bean id = "dataSource" ... />
< bean ... />
< beans profile = "..." >
< bean ...>
</ beans >
</ beans >
|
激活 profile
spring 为我们提供了大量的激活 profile 的方法,可以通过代码来激活,也可以通过系统环境变量、JVM参数、servlet上下文参数来定义 spring.profiles.active 参数激活 profile,这里我们通过定义 JVM 参数实现。
1、ENV方式:
1
|
ConfigurableEnvironment.setActiveProfiles( "test" )
|
2、JVM参数方式:
tomcat 中 catalina.bat(.sh中不用“set”) 添加JAVA_OPS。通过设置active选择不同配置文件
1
|
set JAVA_OPTS= "-Dspring.profiles.active=test"
|
eclipse 中启动tomcat。项目右键 run as –> run configuration–>Arguments–> VM arguments中添加。local配置文件不必上传Git追踪管理
1
|
-Dspring.profiles.active= "local"
|
3、web.xml方式:
1
2
3
4
|
< init-param >
< param-name >spring.profiles.active</ param-name >
< param-value >production</ param-value >
</ init-param >
|
4、标注方式(junit单元测试非常实用):
1
|
@ActiveProfiles ({ "unittest" , "productprofile" })
|
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
原文链接:http://blog.csdn.net/jenny8080/article/details/53185178
相关文章
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
Win10电脑关机太慢怎么办?Win10电脑快速关机方法教程
2025-05-27 49 -
2025-05-27 19
-
2025-05-27 25
-
2025-05-25 15
-
2025-05-29 48