1.結合junit加載數據源
開發、測試、生產都有各自的數據庫,這樣需要配置的數據源不一樣。
每次部署時修改配置過於繁瑣,此時,
可以使用Spring的Profile來配置多個數據源,運行前指定需要加載的數據源即可。 采用spring結合junit做演示,使用oracle和mysql做數據源區別演示,
Profile名指定為oracle和mysql,junit測試時,
使用@ActiveProfiles("mysql"),@ActiveProfiles("oracle")來指定需要加載的數據源。

ps:激活指定Profile,junit使用@ActiveProfiles("oracle")
2.了解Profile
(1)Spring中的Profile定義 Spring中的Profile功能其實早在Spring 3.1的版本就已經出來,
它可以理解為我們在Spring容器中所定義的Bean的邏輯組名稱,
只有當這些Profile被激活的時候,才會將Profile中所對應的Bean注冊到Spring容器中。
舉個更具體的例子,我們以前所定義的Bean,當Spring容器一啟動的時候,
就會一股腦的全部加載這些信息完成對Bean的創建;而使用了Profile之后,
它會將Bean的定義進行更細粒度的划分,將這些定義的Bean划分為幾個不同的組,
當Spring容器加載配置信息的時候,首先查找激活的Profile,
然后只會去加載被激活的組中所定義的Bean信息,
而不被激活的Profile中所定義的Bean定義信息是不會加載用於創建Bean的。
(2)使用Profile
由於我們平時在開發中,通常會出現在開發的時候使用一個開發數據庫,
測試的時候使用一個測試的數據庫,而實際部署的時候需要一個數據庫。
以前的做法是將這些信息寫在一個配置文件中,當我把代碼部署到測試的環境中,
將配置文件改成測試環境;當測試完成,項目需要部署到現網了,
又要將配置信息改成現網的,真的好煩。。。
而使用了Profile之后,我們就可以分別定義3個配置文件,
一個用於開發、一個用戶測試、一個用戶生產,
其分別對應於3個Profile。當在實際運行的時候,
只需給定一個參數來激活對應的Profile即可,
那么容器就會只加載激活后的配置文件,
這樣就可以大大省去我們修改配置信息而帶來的煩惱。
(3)配置Spring profile
在介紹完Profile以及為什么要使用它之后,
下面讓我們以一個例子來演示一下Profile的使用,
這里還是使用傳統的XML的方式來完成Bean的裝配。
第一步:Maven依賴
由於只是做一個簡單演示,因此無需引入Spring其他模塊中的內容,
只需引入核心的4個模塊+測試模塊即可
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!--指定Spring版本,該版本必須等於3.1--> <spring.version>4.2.4.RELEASE</spring.version> <!--指定JDK編譯環境--> <java.version>1.7</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> </plugins> </build>
第二步:例子代碼
package com.panlingxiao.spring.profile.service; /** * 定義接口,在實際中可能是一個數據源 * 在開發的時候與實際部署的時候分別使用不同的實現 */ public interface HelloService { public String sayHello(); }
定義生產環境使用的實現類
package com.panlingxiao.spring.profile.service.produce; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import com.panlingxiao.spring.profile.service.HelloService; /** * 模擬在生產環境下需要使用的類 */ @Component public class ProduceHelloService implements HelloService { //這個值讀取生產環境下的配置注入 @Value("#{config.name}") private String name; public String sayHello() { return String.format("hello,I'm %s,this is a produce environment!", name); } }
模擬在開發環境下需要使用的類
package com.panlingxiao.spring.profile.service.dev; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import com.panlingxiao.spring.profile.service.HelloService; /** * 模擬在開發環境下使用類 */ @Component public class DevHelloService implements HelloService{ //這個值是讀取開發環境下的配置文件注入 @Value("#{config.name}") private String name; public String sayHello() { return String.format("hello,I'm %s,this is a development environment!", name); } }
定義配置Spring配置文件
<?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:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"> <!-- 定義開發的profile --> <beans profile="development"> <!-- 只掃描開發環境下使用的類 --> <context:component-scan base-package="com.panlingxiao.spring.profile.service.dev" /> <!-- 加載開發使用的配置文件 --> <util:properties id="config" location="classpath:dev/config.properties"/> </beans> <!-- 定義生產使用的profile --> <beans profile="produce"> <!-- 只掃描生產環境下使用的類 --> <context:component-scan base-package="com.panlingxiao.spring.profile.service.produce" /> <!-- 加載生產使用的配置文件 --> <util:properties id="config" location="classpath:produce/config.properties"/> </beans> </beans>
說明:
開發使用的配置文件,dev/config.properties
name=Tomcat
生產使用的配置文件,produce/config.properties
name=Jetty
編寫測試類
package com.panlingxiao.spring.profile.test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.panlingxiao.spring.profile.service.HelloService; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath:spring-profile.xml") /* * 使用注冊來完成對profile的激活, * 傳入對應的profile名字即可,可以傳入produce或者dev */ @ActiveProfiles("produce") public class TestActiveProfile { @Autowired private HelloService hs; @Test public void testProfile() throws Exception { String value = hs.sayHello(); System.out.println(value); } }
3.部署時指定Profile
第一步:
配置不同環境的配置文件
建立對應的環境目錄,我這里有三個環境分別是,
dev/test/pro 對應 開發/測試/生產。
建好目錄后將相應的配置文件放到對應的環境目錄中

第二步:配置 pom.xml 設置 profile
這里通過 activeByDefault
將開發環境設置為默認環境。
如果你是用 idea 開發的話,在右側 maven projects > Profiles 可以勾選對應的環境。
<profiles>
<profile>
<!-- 本地開發環境 -->
<id>dev</id>
<properties>
<profiles.active>dev</profiles.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!-- 測試環境 -->
<id>test</id>
<properties>
<profiles.active>test</profiles.active>
</properties>
</profile>
<profile>
<!-- 生產環境 -->
<id>pro</id>
<properties>
<profiles.active>pro</profiles.active>
</properties>
</profile>
</profiles>
第三步:打包時根據環境選擇配置目錄
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.1.0</version> <configuration> <warSourceExcludes> config/test/**,config/pro/**,config/dev/** </warSourceExcludes> <webResources> <resource> <directory>src/main/webapp/config/${profiles.active}</directory> <targetPath>config</targetPath> <filtering>true</filtering> </resource> </webResources> </configuration> </plugin>
第四步:根據環境打包
## 開發環境打包 mvn clean package -P dev ## 測試環境打包 mvn clean package -P test ## 生產環境打包 mvn clean package -P pro
4.配置profile (yml文件)
4.1.全局配置文件 (1)application.properties或者application.yml (2)固定位置: 配置文件存放在src/main/resources目錄或者類路徑/config下; 4.2 (1)Profile定義: Profile是Spring對不同環境提供不同配置功能的支持, 可以通過激活,指定參數等方式快速切換環境; (2)多Profile文件 主配置文件的文件名,可以是application-{profile}.properties; 默認使用application.properties的配置; 在配置文件中,使用application.profiles.active=dev激活使用; (3)yml支持的多文檔塊方式
server: port: 8081 spring: profiles: active: prod # 激活生產環境 --- # 測試環境 server: port: 8083 spring: profiles: dev --- # 生產環境 server: port: 8084 spring: profiles: prod
學習來源:
//測試時加載數據源
https://www.cnblogs.com/alphajuns/p/12622869.html
//了解Profile
https://blog.csdn.net/u010947534/article/details/90440769
//部署時指定Profile
https://www.cnblogs.com/lionsblog/p/10214751.html
//配置profile properties文件
https://blog.csdn.net/daibang2182/article/details/80757180
//配置profile yml文件
https://www.cnblogs.com/linkworld/p/9135650.html