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

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

方法一:配置profile bean

@Configurationpublic classDataSourceConfig {//Spring 引入@Profile 制定某个bean属于哪个profile//在方法级别上使用@Profile注解  @Bean@Profile("dev")publicDataSource embeddedDataSource() {return newEmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).addScript("classpath:schema.sql").addScript("classpath:dev-data.sql").build();}@Bean@Profile("prod")publicDataSource embeddedDataSourceDev() {return newEmbeddedDatabaseBuilder().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的默认值。

测试

packageprofiles;import static org.junit.Assert.*;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.util.List;importjavax.sql.DataSource;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.jdbc.core.JdbcTemplate;importorg.springframework.jdbc.core.RowMapper;importorg.springframework.test.context.ActiveProfiles;importorg.springframework.test.context.ContextConfiguration;importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;importcom.myapp.DataSourceConfig;public classDataSourceConfigTest {@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes=DataSourceConfig.class)@ActiveProfiles("dev")public static classDevDataSourceTest {@AutowiredprivateDataSource dataSource;@Testpublic voidshouldBeEmbeddedDatasource() {assertNotNull(dataSource);JdbcTemplate jdbc= newJdbcTemplate(dataSource);List<String> results = jdbc.query("select id, name from Things", new RowMapper<String>() {@Overridepublic String mapRow(ResultSet rs, int rowNum) throwsSQLException {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 classProductionDataSourceTest_XMLConfig {@AutowiredprivateDataSource dataSource;@Testpublic voidshouldBeEmbeddedDatasource() {assertNotNull(dataSource);JdbcTemplate jdbc= newJdbcTemplate(dataSource);List<String> results = jdbc.query("select id, name from Things", new RowMapper<String>() {@Overridepublic String mapRow(ResultSet rs, int rowNum) throwsSQLException {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

Spring 环境与profile(一)——超简用例相关推荐

  1. Spring小知识——profile配置

    文章目录 引言 介绍 Spring配置步骤如下 第一步:编写三个环境的Spring配置文件如下 第二步:配置Spring的主配置文件 第三步:编写测试代码 第四步:报错原因分析 总结 引言 工作中我们 ...

  2. springboot配置多项目下统一切换不同环境变量profile办法

    springboot配置多项目下统一切换不同环境变量profile办法 springboot 适合于微服务那种多项目开发,每一个小项目就是一个springboot 项目,比如我们这样: 问题发生: 我 ...

  3. spring 容器的 profile 功能

    为什么80%的码农都做不了架构师?>>>    软件开发的一般流程为工程师开发 -> 测试 -> 上线,因此就涉及到三个不同的环境,开发环境.测试环境以及生产环境,通常这 ...

  4. Spring框架学习笔记,超详细!!(4)

    Java小白开始学习Spring框架,一方面,跟着视频学习,并记录下学习笔记,方便以后复习回顾.另一方面,发布学习笔记来约束自己,学习路程还很遥远,继续加油坚持!!!希望能帮助到大家! 另外还有我的牛 ...

  5. Spring Boot配置 profile

    Spring Boot配置 profile 配置profile作用:我们在开发Spring Boot应用时,通常同一套程序会被安装到不同环境,比如:开发.测试.生产等.其中数据库地址.服务器端口等等配 ...

  6. Spring环境搭建,IoC容器初体验~

    由于最近的任务是关于IoC配置文件格式的转换,所以需要从Spring的IoC容器开始学起,今天根据网上的介绍搭建了Spring环境,并对其IoC容器进行了初体验.文章中涉及到的软件以及推荐的一本关于S ...

  7. spring boot api文档_精讲RestTemplate第1篇-在Spring或非Spring环境下如何使用

    一.什么是 RestTemplate? RestTemplate是执行HTTP请求的同步阻塞式的客户端,它在HTTP客户端库(例如JDK HttpURLConnection,Apache HttpCo ...

  8. spring 环境配置

    搭建Spring开发环境并编写第一个Spring小程序 一.前面,我写了一篇Spring框架的基础知识文章,里面没讲到如何配置Spring开发环境,今天就来讲一下,如果大家不知道怎么下载Spring软 ...

  9. IoC、Spring 环境搭建、Spring 创建对象的三种方式、DI

    二.IoC 中文名称:控制反转 英文名称:(Inversion of Control) 3.I oC 是什么? 3.1 IoC 完成的事情原先由程序员主动通过 new 实例化对象事情,转交给 Spri ...

最新文章

  1. 读文件 —— 读写配置文件
  2. IBM技术大会2005
  3. 2016.6.23 随笔———— AJAX
  4. Webclient UI view里Javascript的注释问题
  5. sketch浮动布局_使用智能布局和调整大小在Sketch中创建更好的可重用符号
  6. php导入excel到mysql的方法
  7. java连接并操作redis_java 使用 jedis 连接 redis 并进行简单操作
  8. python实现的摩斯电码解码\编码器
  9. 力扣—— 79/212. 单词搜索
  10. 【Python】【有趣的模块】【systimeos】
  11. oracle DML错误日志(笔记)
  12. Nginx源码阅读 --- http模块 --- TCP连接过程
  13. 使用maven将jar包下载到本地仓库
  14. FYI-django数据库操作-外键
  15. 小孩子爱玩手机学计算机编程好吗,学习编程教育对孩子的哪些重要意义
  16. Quartz简介及应用场景
  17. Java获取今天 开始和结束时间
  18. python绘制饼图
  19. kubeadmin部署k8s
  20. 如何部署JSP应用到阿里云服务器上(一)

热门文章

  1. oracle00109,ORA-01034: 、ORA-01078: 和 LRM-00109: 的解决方法,ora-01034ora-01078
  2. 一个本地分支能关联两个远程仓库吗_使用git分支保存hexo博客源码到github
  3. MIUI 13:带来全新小部件,新增三大隐私保护功能等
  4. APP技巧:推荐6款超级实用的APP软件,赶快下载试试吧!
  5. linux netbeans 中文乱码,浅谈Linux Netbeans字体反锯齿处理
  6. c 跨平台android,Unity 使用C/C++ 跨平台终极解决方式(PC,iOS,Android,以及支持C/C++的平台)...
  7. 什么字体字母和数字大小一样_字母和字体如何适应我们的屏幕
  8. lottie 动画_使用After Effects和Lottie制作网络动画而不会损失质量
  9. qq空间网页设计_网页设计中负空间的有效利用
  10. 【热点】React18正式版发布,未来发展趋势是?