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 ...
随机推荐
- 如何处理用代码创建SD Sales order时遇到的错误消息KI 180
错误消息KI 180:You must enter a company code for transaction Create sales document 代码: REPORT zcreate_so ...
- 分享个谷歌浏览器下的一款插件PostMan
用作POST GET调试非常好用 先下载谷歌浏览器 然后在应用里搜索安装即可
- OpenGL进阶演示样例1——动态画线(虚线、实线、颜色、速度等)
用OpenGL动态绘制线段.事实上非常easy,但到如今为止.网上可參考资料并不多. 于是亲自己主动手写一个函数,方便动态绘制线段.代码例如以下: #include<GL/glu ...
- springmvc中校验框架(hibernate)
JSR303定义的校验类型 <dependency> <groupId>org.hibernate</groupId> <artifactId>hibe ...
- 中小学信息学奥林匹克竞赛-理论知识考点--ASCII
ASCII表说白了就是一张表. 表中记录着:字符 和 数字 的对应关系.比如:字符0对应的ASCII码是48,A对应的是65,a对应的是97. 只要记住这三个,其它的数字,大写,小写字母的ASCII码 ...
- System.IO 二
接着上篇的来 System.IO FileSystemWatcher 指向这个签名的方法 可以监听目录发生了什么事件 例如: static void Main(string[] args) ...
- C#类的实例
一.C#类的实例 类的实例其实就是类的对象.定义类之后即可声明类的对象,声明对象之后便可以访问对象的成员了. 语法格式如下: 类名 对象名; // 声明对象对象名 = new 类名() ...
- 批量删除xml文件中的<?xml version="1.0" ?>
#!/bin/shcd 'home/usrname/'ls cd '/home/usrname/VOC2007/Annotations/' for file in `ls /home/usrname/ ...
- javabeans 内省 introspector BeanUtils
javaBeans 属性的概念 不只是字段,而是其get set 方法 且该get方法有返回值的称为属性,继承Object类的getClass方法 package com.swift.demo1; p ...
- 【清真dp】cf1144G. Two Merged Sequences
成就:赛后在cf使用错误的贪心通过一题 成就:在cf上赛后提交hack数据 成就:在cf上赛后hack自己 题目大意 有一长度$n \le 2\times 10^5$的序列,要求判断是否能够划分为一个 ...