本地、测试、开发、产品等不同环境文件配置

现象

  如果在开发时进行一些数据库测试,希望链接到一个测试的数据库,以避免对开发数据库的影响。

  开发时的某些配置比如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")
  • 1
  • 1

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"
  • 1

3、web.xml方式:

<init-param>
<param-name>spring.profiles.active</param-name>
<param-value>production</param-value>
</init-param>
4、标注方式(junit单元测试非常实用):
@ActiveProfiles({"unittest","productprofile"})

spring profile 多环境配置管理的更多相关文章

  1. 配置多个数据源,spring profile 多环境配置管理

    针对生产环境,测试环境,以及本地调试开发有时会配置多套数据库,在一个数据配置文件进行修改,往往有时发布到生成环境会忘记修改,或者本地调试时还是生产环境的库,会导致生产环境数据被污染. ps--刚开始配 ...

  2. Spring Boot入门(二):使用Profile实现多环境配置管理&如何获取配置文件值

    在上一篇博客Spring Boot入门(一):使用IDEA创建Spring Boot项目并使用yaml配置文件中,我们新建了一个最原始的Spring Boot项目,并使用了更为流行的yaml配置文件. ...

  3. Maven 整合 spring profile实现多环境自动切换

    Maven 整合 spring profile实现多环境自动切换 时间:2014-03-19 15:32来源:Internet 作者:Internet 点击:525次 profile主要用在项目多环境 ...

  4. Spring boot项目maven的profile多环境配置不自动替换变量的问题解决

    Spring boot项目maven的profile多环境配置不自动替换变量的问题解决   在网上找了好久,配置都很简单,可是我的程序就是不能自动替换变量,最终单独测试,发现原来是引用spring b ...

  5. Spring.profile配合Jenkins发布War包,实现开发、测试和生产环境的按需切换

    前两篇不错 Spring.profile实现开发.测试和生产环境的配置和切换 - Strugglion - 博客园https://www.cnblogs.com/strugglion/p/709102 ...

  6. Spring.profile实现开发、测试和生产环境的配置和切换

    软件开发过程一般涉及“开发 -> 测试 -> 部署上线”多个阶段,每个阶段的环境的配置参数会有不同,如数据源,文件路径等.为避免每次切换环境时都要进行参数配置等繁琐的操作,可以通过spri ...

  7. 通过Spring profile方式实现多环境部署

    1 多环境部署 在实际软件开发和部署过程中,我们的软件往往需要在不同的运行环境中运行.例如,各个环境数据库地址不同,需要单独配置.spring高级装备中提供profile,来支持多环境部署. 1.1 ...

  8. spring Profile 为不同环境提供不同的配置支持

    说明 Profile为在不同环境下使用不同的配置提供了支持(开发环境下的配置和生产环境下的配置肯定是不同的, 例如, 数据库的配置) . 在spring开发中用@Profile 注解使用来选择行配置系 ...

  9. Spring Cloud Alibaba | Nacos配置管理

    目录 Spring Cloud Alibaba | Nacos配置管理 1. pom.xml 项目依赖 2. 在 bootstrap.properties 中配置 Nacos server 的地址和应 ...

随机推荐

  1. LeetCode Zigzag Iterator

    原题链接在这里:https://leetcode.com/problems/zigzag-iterator/ 题目: Given two 1d vectors, implement an iterat ...

  2. Vcenter server 5.5添加用户角色及分配权限

    角色:各种角色定义了对此角色可操作细节的权限组合. 用户权限:用户权限是对ESXi 5.0中的对象实例(如ESXi 5.0主机,虚拟机,存储,网络等)进行权限的分配.通过在这些对象上绑定“用户+角色” ...

  3. openfalcon客户端自定义push 传输到transfer

    . linux客户端部署agent . 编写脚本,比如: #!/usr/bin/env python #!-*- coding:utf8 -*- import requests import time ...

  4. 弹出框以及提示插件lghdialog.js的使用

    以下使用方法 swfupload的使用

  5. Linux:安装OpenSSH-Server E:Package openssh-server has no installation candidate

    $sduo apt-get install openssh-server Reading package lists… Done Building dependency tree Reading st ...

  6. C/C++ 结构体 简单输入输出

    #include <stdio.h> #include <stdlib.h> struct student{ int num; ]; double dec; }; int ma ...

  7. Android之下载管理者

    public interface HttpDownloader { public void setDownloadManager(HttpDownloadManager manager); publi ...

  8. javascript的类、委托、事件

    javascript中的类: javascript中的类 );         p2.show();                  //注:Javascript中没有真正的方法重载 看起来很简单吧 ...

  9. animation 的属性一共有 6 个值,详细介绍在此

    animation 属性是一个简写属性,用于设置六个动画属性: animation-name animation-duration animation-timing-function animatio ...

  10. 利用BMFont和NGUI制作字体集

    Unity中常常需要制作字体,也算是Unity的基本优势吧!其实质就是BMFont和NGUI制作字体.这里把步骤介绍一下: 1.先下载BMFont这个工具 2.Font Settings  设置:(1 ...