Spring 环境与profile(一)——超简用例

什么是profile,为什么需要profile?

在开发时,不同环境(开发、联调、预发、正式等)所需的配置不同导致,如果每改变一个环境就更改配置不但麻烦(修改代码、重新构建)而且容易出错。Spring提供了解决方案。

方法一:配置profile bean

@Configuration
public class DataSourceConfig {//Spring 引入@Profile 制定某个bean属于哪个profile//在方法级别上使用@Profile注解
@Bean@Profile("dev")public DataSource embeddedDataSource() {return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).addScript("classpath:schema.sql").addScript("classpath:dev-data.sql").build();}@Bean@Profile("prod")public DataSource embeddedDataSourceDev() {return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).addScript("classpath:schema.sql").addScript("classpath:prod-data.sql").build();} }

在同一个类的不同方法上使用@Profile注解与@Bean一起使用

方法二:在XML中配置bean

与方法一等价的方式

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"xmlns:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="
    http://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee.xsdhttp://www.springframework.org/schema/jdbchttp://www.springframework.org/schema/jdbc/spring-jdbc.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><beans profile="dev"><jdbc:embedded-database id="dataSource" type="H2"><jdbc:script location="classpath:schema.sql" /><jdbc:script location="classpath:dev-data.sql" /></jdbc:embedded-database></beans><beans profile="prod"><jdbc:embedded-database id="dataSource" type="H2"><jdbc:script location="classpath:schema.sql" /><jdbc:script location="classpath:prod-data.sql" /></jdbc:embedded-database></beans>
</beans>

激活profile

Spring在确定哪个profile处于激活状态时,需要依赖两个独立属性:sping.profiles.active和spring.profiles.default。Spring提供了@ActiveProfiles用来指定运行测试时要激活哪个profile,如果没有指定sping.profiles.active,会采用spring.profiles.default的默认值。

测试

package profiles;import static org.junit.Assert.*;import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;import javax.sql.DataSource;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.myapp.DataSourceConfig;public class DataSourceConfigTest {@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes=DataSourceConfig.class)@ActiveProfiles("dev")public static class DevDataSourceTest {@Autowiredprivate DataSource dataSource;@Testpublic void shouldBeEmbeddedDatasource() {assertNotNull(dataSource);JdbcTemplate jdbc = new JdbcTemplate(dataSource);List<String> results = jdbc.query("select id, name from Things", new RowMapper<String>() {@Overridepublic String mapRow(ResultSet rs, int rowNum) throws SQLException {return rs.getLong("id") + ":" + rs.getString("name");}});assertEquals(1, results.size());assertEquals("1:A", results.get(0));}}@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:datasource-config.xml")@ActiveProfiles("prod")public static class ProductionDataSourceTest_XMLConfig {@Autowiredprivate DataSource dataSource;@Testpublic void shouldBeEmbeddedDatasource() {assertNotNull(dataSource);JdbcTemplate jdbc = new JdbcTemplate(dataSource);List<String> results = jdbc.query("select id, name from Things", new RowMapper<String>() {@Overridepublic String mapRow(ResultSet rs, int rowNum) throws SQLException {return rs.getLong("id") + ":" + rs.getString("name");}});assertEquals(1, results.size());assertEquals("1:B", results.get(0));}}}

代码

地址

转载于:https://www.cnblogs.com/kaituorensheng/p/8763683.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/280656.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Django04-1: ORM增删改查

ORM 增删改查 一、字段增加 #终端输入 1.model里添加字段&#xff0c; 2.执行迁移命令。 3.终端里输入默认值&#xff0c;继续执行迁移命令。 #允许为空 再nulltrue&#xff0c;终端不需要输入默认值 #设置默认值 defalult‘xxxx‘ 二、字段修改 1.直接修改代码&…

Comcast以纯文本泄露客户Wi-Fi登录信息,立即更改密码

A Comcast Xfinity website was leaking Wi-Fi names and passwords, meaning now is a good time to change your Wi-Fi passcode. Comcast Xfinity网站泄漏了Wi-Fi名称和密码&#xff0c;这意味着现在是更改Wi-Fi密码的好时机。 The site, intended to help new customers se…

SpringBoot详解(一)-快速入门

SpringBoot详解系列文章&#xff1a;SpringBoot详解&#xff08;一&#xff09;-快速入门SpringBoot详解&#xff08;二&#xff09;-Spring Boot的核心SpringBoot详解&#xff08;三&#xff09;-Spring Boot的web开发SpringBoot详解&#xff08;四&#xff09;-优雅地处理日志…

龙芯上跑WTM,为国产化做点贡献

点击上方蓝字关注我哦“信创”&#xff0c;是一项国家战略&#xff0c;即信息技术应用创新产业&#xff0c;它是数据安全、网络安全的基础&#xff0c;也是新基建的重要组成部分。信创从名称上来看本意指向创新&#xff0c;但是自从漂亮国亲手撕碎了“科技没有国界”的谎言之后…

Class与Style绑定

对于数据绑定&#xff0c;一个常见的需求是操作元素的class列表和它的内联样式。因为它们都是attribute&#xff0c;我们可以用v-bind处理它们&#xff1a;只需要计算出表达式最终的字符串。不过&#xff0c;字符串拼接麻烦又易错。因此&#xff0c;在v-bind用于class和style时…

PHP安装之configure的配置参数

1、生成环境安装配置如下 要求安装如下库&#xff1a; imagickgdmysqlmysqlimysqlndphalconPharsoapsocketsxwebxsvczipzlib 具体查看 vim php-config 就可以知道是如何配置的 --prefix/home/php --with-config-file-path/home/php/etc --with-mysql --with-pdo-oci --with-ope…

Django05: 请求生命周期流程图/路由层

请求生命周期流程图 扩展知识&#xff1a; 缓存数据库 路由层 路由匹配 url(r^test/, views.test), 1. 第一个参数是正则匹配。 只要第一个匹配了&#xff0c;就不会执行下面。 输入url会默认加斜杠&#xff0c;django会重定向 a. 一次匹配不行 b. url再加斜杠匹配 可以…

facebook 分享页面_Facebook个人资料,页面和组之间有什么区别?

facebook 分享页面Facebook is used by a lot of different people for a lot of different things, so it’s only natural that Facebook would have different sets of features for each of them. There are three main ways you can use Facebook: with a regular Profile…

zabbix运行脚本监控ggsci报错

/u01/app/oracle/oracle/ogg/ggsci: error while loading shared libraries: libdb-6.1.so: cannot open shared object file: No such file or directory增加脚本环境变量设置PATH$PATH:$HOME/binexport ORACLE_BASE/u01/app/oracleexport ORACLE_HOME$ORACLE_BASE/11/db_1exp…

一句话设计原则

面向对象的可复用设计&#xff08; Object Oriented Design / OOD&#xff09; 1. 开闭原则 (Open Closed Principle) 对扩展开放&#xff0c;对修改关闭 2. 里氏代换原则(LSP) 1.可以使用基类的地方&#xff0c;其子类必然也能使用 2.并且原功能不会受到任何影响 -- 经典案例,…

postman--安装及Interceptor插件

1. 官网安装&#xff08;看网速-我下载的时候一直下载失败&#xff09;打开官网&#xff0c;https://www.getpostman.com选择ios或者win 2. 非官网安装 https://pan.baidu.com/s/1mstsimqO3ZC5m9z8czxVnA 密码&#xff1a;q6yp 安装postman 3.需要安装分享的蓝灯安装包&#xf…

亚马逊标题自动抓取_如何为您的家人提供自动Amazon礼品卡津贴

亚马逊标题自动抓取When your kids move away to go to school, they’ll probably phone home every once in a while to ask for money. If they shop a lot on Amazon (and they probably do), you can expedite that process by setting up an automatically recurring dep…

Django04-2: ORM关系表\字段补充

一、表与表关系 一对多 多对多 一对一 图书表 出版社 作者表 作者详情表 出版社 和 图书表 关系 一对多 外键字段在多的一方 book 图书表 和 作者表 关系 多对多 需要创建第三张表 作者表 和 作者详情表 关系 一对一 #创建表关系 先将基表创建 再添加外键字段 一对多…

我 与 TDesignBlazor 的故事

前言作者打拼了 .NET 十多年&#xff0c;属于全栈应用类型的工程师&#xff0c;特别是对于前端的技术情有独钟&#xff0c;从纯js到jquery&#xff0c;从bootstrap到自己写css&#xff0c;从web到winform&#xff0c;还写过一段时间的knockout.js&#xff0c;以至于公司里的前端…

实验数据

1.整段deng音频200多秒 2.加xx(1000:1480)之后 转载于:https://www.cnblogs.com/20179302yzl/p/10270632.html

25个好用的Shell脚本常用命令分享

1.列出所有目录使用量&#xff0c;并按大小排序。复制代码 代码如下:ls|xargs du -h|sort -rn #不递归下级目录使用du -sh2.查看文件排除以#开关和空白行&#xff0c;适合查看配置文件。复制代码 代码如下:egrep -v "^#|^$" filenamesed /#.*$/d; /^ *$/d3.删除空格…

mysql中查询一个字段属于哪一个数据库中的哪一个表的方式

mysql中查询一个字段具体是属于哪一个数据库的那一张表&#xff1a;用这条语句就能查询出来,其中 table_schema 是所在库, table_name 是所在表 --mysql中查询某一个字段名属于哪一个库中的哪一张表 select table_schema,table_name from information_schema.columns where col…

macos剪切_如何使用macOS的内置“ Kill and Yank”作为替代剪切和粘贴

macos剪切Everyone knows about cutting and pasting by now. But did you know that your Mac sort of has a second clipboard known as kill and yank? 现在&#xff0c;每个人都知道剪切和粘贴。 但是您是否知道Mac上还有第二个剪贴板&#xff0c;称为“ kill and yank”&…

ExtJS 折线图趟过的坑

问题&#xff1a; 1、根据条件检索后绘制折线图&#xff0c;之前的坐标没有清除如图 解决方案&#xff1a; 在绘制之前&#xff0c;清空坐票&#xff1a; leftLine.surface.removeAll(); leftLine.redraw(false); 完整代码如下 storeBar.load({params: { SDate: bTime, EDate: …

EventBus的实现

EventBus概要 EventBus是消息传递的一种方式&#xff0c;基于一个消息中心&#xff0c;订阅和发布消息的模式。这种方式的实现不仅仅局限于前端&#xff0c;在iOS中的消息消息中心也是如此实现。 设计模式&#xff1a;订阅者发布者模式&#xff0c;这种设计模式在前端很常见。A…