什么是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/jee
http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/beans
http://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 {
@Autowired
private DataSource dataSource; @Test
public void shouldBeEmbeddedDatasource() {
assertNotNull(dataSource);
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
List<String> results = jdbc.query("select id, name from Things", new RowMapper<String>() {
@Override
public 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 {
@Autowired
private DataSource dataSource; @Test
public void shouldBeEmbeddedDatasource() {
assertNotNull(dataSource);
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
List<String> results = jdbc.query("select id, name from Things", new RowMapper<String>() {
@Override
public 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));
}
} }

代码

地址

Spring 环境与profile(一)——超简用例的更多相关文章

  1. Spring 环境与profile(二)——Properties with Spring

    1. 简述 Spring profile用例,分3个场景(Test, Dev, Prod)相对Spring 环境与profile(一)——超简用例多了根据具体的profile获取对应的Properti ...

  2. Spring 环境与profile(三)——利用maven的resources、filter和profile实现不同环境使用不同配置文件

    基本概念 profiles定义了各个环境的变量id filters中定义了变量配置文件的地址,其中地址中的环境变量就是上面profile中定义的值 resources中是定义哪些目录下的文件会被配置文 ...

  3. 【Spring】使用@Profile注解实现开发、测试和生产环境的配置和切换,看完这篇我彻底会了!!

    写在前面 在实际的企业开发环境中,往往都会将环境分为:开发环境.测试环境和生产环境,而每个环境基本上都是互相隔离的,也就是说,开发环境.测试环境和生产环境是互不相通的.在以前的开发过程中,如果开发人员 ...

  4. Spring boot 的profile功能如何实现多环境配置自动切换

    通常服务端应用开发需要经过以下几个流程: 开发 -> 测试 -> RC验证 -> 上线 这就涉及到四个不同的环境,开发环境.测试环境.RC环境以及生产环境,为了避免不同环境之间相互干 ...

  5. 【Java Web开发学习】Spring环境profile

    [Java Web开发学习]Spring 环境profile 转载:http://www.cnblogs.com/yangchongxing/p/8890702.html 开发.测试.生产环境往往是不 ...

  6. Spring Boot 之 Profile --快速搞定多环境使用与切换

    Spring Profile是Spring3引入的概念,主要用在项目多环境运行的情况下,通过激活方式实现多环境切换,省去多环境切换时配置参数和文件的修改,并且Spring profile提供了多种激活 ...

  7. 【Spring源码解析】—— 简单工厂模式的BeanFactory的超简版实现

    一.什么是简单工厂模式 设计模式的核心是“分工”,通过分工将对象与职责划分的更细化,进而提升系统设计的可扩展性,使其更容易维护. 开闭原则:对扩展开放,对修改关闭:要增加一个新的处理逻辑,可以开一个新 ...

  8. 新入手服务器不会玩?抢占式实例服务器教程,从零搭建tomcat超简流程

    新入手服务器不会玩?抢占式实例服务器教程,从零搭建tomcat超简流程 相信很多新人入手Linux服务器后,一脸无奈,这黑框框究竟能干啥?忽觉巨亏血亏不是? 这里面门道可不是你想象中的那么点,简则服务 ...

  9. [Spring Boot 系列] 集成maven和Spring boot的profile功能

    由于项目的需要, 今天给spirng boot项目添加了profile功能.再网上搜索了一圈,也没有找到满意的参考资料,其实配置并不难,就是没有一个one stop(一站式)讲解的地方,所以有了写这篇 ...

随机推荐

  1. Tarjan求缩点化强连通图

    Describe: 求一个有向图加多少条边可以变成一个强连通图 Solution: Tarjan缩点染色后,判断出度和入度,所有点的出度 = 0 的和 和 入度 = 0 的和的最大值即为所求. 缩点染 ...

  2. Django用户验证框架

    一  分析源码 User   Django的标准库存放在 django.contrib 包中.每个子包都是一个独立的附加功能包. 这些子包一般是互相独立的,不过有些django.contrib子包需要 ...

  3. ASP.NET Core 2.1 源码学习之 Options[3]:IOptionsMonitor

    前面我们讲到 IOptions 和 IOptionsSnapshot,他们两个最大的区别便是前者注册的是单例模式,后者注册的是 Scope 模式.而 IOptionsMonitor 则要求配置源必须是 ...

  4. 【转】MySQL表名大小写敏感导致的问题

    原文地址:https://blog.csdn.net/postnull/article/details/72455768 最近在项目中遇到一个比较奇怪的小问题.在开发过程中自己测试没有问题,但是提测后 ...

  5. CF1146H Satanic Panic

    题目传送门 Description 给定二维平面内\(n\)个点\((n\leq 300)\),求能组成五角星(不要求正五角星)的五元组个数. Solution 一道小清新的寄蒜几盒计算几何题,代码不 ...

  6. SQL Server清理大日志文件方法 不分离数据库 执行SQL语句即可

    SQL 2008清空日志的SQL语句如下: USE[master] GO ALTER DATABASE 要清理的数据库名称 SET RECOVERY SIMPLE WITH NO_WAIT GO AL ...

  7. fastjson 反序列化漏洞笔记,比较乱

    现在思路还是有点乱,希望后面能重新写 先上pon.xml 包 <?xml version="1.0" encoding="UTF-8"?> < ...

  8. postgresql-脏页和缓存失效

    脏页和缓存失效 https://www.cnblogs.com/flying-tiger/p/7885478.html Dirty pages and cache invalidation 我们一直在 ...

  9. python3.6使用f-string来格式化字符串

    这里的f-string指的是以f或F修饰的字符串,在字符串中使用{}来替换变量,表达式和支持各种格式的输出.详细的格式化定义可以看官方文档 >>> a, b = 30, 20 > ...

  10. EXECUTE 后的事务计数指示缺少了 COMMIT 或 ROLLBACK TRANSACTION 语句。上一计数 = 1,当前计数 = 2

    理解这一句话: 一个begin tran会增加一个事务计数器,要有相同数量的commit与之对应,而rollback可以回滚全部计数器 这个错误一般是出现在嵌套事务中. 测试环境 sql 2008 例 ...