spring 笔记3: Spring 多环境配置文件切换
使用Spring进行开发时,需要面对不同的运行环境,比如开发环境、测试环境、生产环境等。大多时候不同的环境需要不同的配置文件。网上很多资料都是使用Spring的Bean definition profiles 功能来实现(https://docs.spring.io/spring/docs/4.2.4.RELEASE/spring-framework-reference/html/beans.html#beans-definition-profiles)。这种方式就会出现同一个Bean要正对不同的环境分别声明,即繁琐,又造成相同的配置要重复很多遍。
比如:需要在vmoption 里面增加 -Dspring.profiles.active=dev
<beans profile="dev">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:properties/config_dev.properties</value>
</list>
</property>
</bean> </beans>
<beans profile="test">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:properties/config_test.properties</value>
</list>
</property>
</bean> </beans>
<beans profile="uat">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:properties/config_uat.properties</value>
</list>
</property>
</bean> </beans>
<beans profile="product">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:properties/config_product.properties</value>
</list>
</property>
</bean> </beans>
相比之下,我更喜欢这种方式:
需要vm option里面新增:-Denv=_rel。 如果没有新增属性,那么默认加载的是文件mybatis/jdbc.properties 。
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:mybatis/jdbc${env:}.properties</value>
<value>classpath*:cache/radis${env:}.properties</value>
<value>classpath*:applicationConfig${env:}.properties</value>
<value>classpath*:rabbitmq/rabbitmq${env:}.properties</value>
<value>classpath*:redisson/redisson${env:}.properties</value>
<value>classpath*:webservice/webservice.properties</value>
<value>classpath*:apiclient${env:}.properties</value>
<value>classpath*:httprequest${env:}.properties</value> </list>
</property>
</bean>
仅就实现配置文件切换来说,两种方式中我倾向于后者,更简洁明了。当然前者有些功能是后者无法实现的,比如针对不同的环境有选择性的实例化对象。
spring 笔记3: Spring 多环境配置文件切换的更多相关文章
- springboot笔记05——profile多环境配置切换
前言 一个应用程序从开发到上线,往往需要经历几个阶段,例如开发.测试.上线.每个阶段所用到的环境的配置可能都是不一样的,Springboot 应用可以很方便地在各个环境中对配置进行切换.所以,今天主要 ...
- SpringBoot 多环境配置文件切换
背景 很多时候,我们项目在开发环境和生成环境的环境配置是不一样的,例如,数据库配置,在开发的时候,我们一般用测试数据库,而在生产环境的时候,我们是用正式的数据,这时候,我们可以利用profile在不同 ...
- Spring笔记(6) - Spring的BeanFactoryPostProcessor探究
一.背景 在说BeanFactoryPostProcessor之前,先来说下BeanPostProcessor,在前文Spring笔记(2) - 生命周期/属性赋值/自动装配及部分源码解析中讲解了Be ...
- Spring笔记1——Spring起源及其核心技术
Spring的作用 当我们使用一种技术时,需要思考为什么要使用这门技术.而我们为什么要使用Spring呢?从表面上面SSH这三大框架中,Struts是负责MVC责任的分离,并且提供为Web层提供诸如控 ...
- Spring笔记(4) - Spring的编程式事务和声明式事务详解
一.背景 事务管理对于企业应用而言至关重要.它保证了用户的每一次操作都是可靠的,即便出现了异常的访问情况,也不至于破坏后台数据的完整性.就像银行的自助取款机,通常都能正常为客户服务,但是也难免遇到操作 ...
- spring笔记3 spring MVC的基础知识3
4,spring MVC的视图 Controller得到模型数据之后,通过视图解析器生成视图,渲染发送给用户,用户就看到了结果. 视图:view接口,来个源码查看:它由视图解析器实例化,是无状态的,所 ...
- Spring笔记(7) - Spring的事件和监听机制
一.背景 事件机制作为一种编程机制,在很多开发语言中都提供了支持,同时许多开源框架的设计中都使用了事件机制,比如SpringFramework. 在 Java 语言中,Java 的事件机制参与者有3种 ...
- spring笔记6 spring IOC的中级知识
1,spring ioc的整体流程,xml配置 spring ioc初始化的流程结合上图 步骤编号 完成的工作 1 spring容器读取配置文件,解析称注册表 2 根据注册表,找到相应的bean实现类 ...
- spring笔记4 spring MVC的基础知识4
//todo 5,spring MVC的本地化解析,文件上传,静态资源处理,拦截器,异常处理等 spring MVC 默认使用AcceptHeaderLocalResolver,根据报文头的Accep ...
随机推荐
- leetcode: 复杂度
1. single-number Given an array of integers, every element appears twice except for one. Find that s ...
- github设置添加SSH(转载自:破男孩)
注:本文来源于 破男孩 博客(http://www.cnblogs.com/ayseeing/p/3572582.html)能切实解决问题. 很多朋友在用github管理项目的时候,都是直接使用htt ...
- 去重算法-hash-set
Well, as Bavarious pointed out in a comment, Apple's actual CoreFoundation source is open and availa ...
- 在matlab中查看变量的数据类型
>> x = x = >> class(x) ans = double
- List<T>转换为二维数组
public <T> Object[][] toArrays(List<T> data){ Object[][] o=new Object[data.size()][20]; ...
- 【iOS】史上最全的iOS持续集成教程 (上)
:first-child{margin-top:0!important}.markdown-body>:last-child{margin-bottom:0!important}.markdow ...
- 【Python3】操作文件,目录和路径
1.遍历文件夹和文件 Python代码 import os import os.path rootdir = "d:/test" for parent,dirnames,fi ...
- 【jQeury】input输入框状态,input事件,blur事件,focus事件
//输入框正在输入时 $("#test1").on('input',function(){ alert('正在输入'); }) //输入框得到焦点时 $("#test2& ...
- 在ubuntu上安装subline
Sublime Text is a most popular, lightweight and smart cross-platform text and source code editor wit ...
- python之doctest的用法
doctest是python自带的一个模块,你可以把它叫做“文档测试”(doctest)模块. doctest的使用有两种方式:一个是嵌入到python源中.另一个是放到一个独立文件. doctest ...