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.pswp.cn/news/280656.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/280656.shtml
英文地址,請注明出處:http://en.pswp.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…