如何在@SpringBootTest中動態地啟用不同的profiles

實現步驟

  • 測試類標注@ActiveProfiles(resolver = ProfilesResolver.class)
  • 自定義類 ProfilesResolver 實現接口 ActiveProfilesResolver,并實現接口中唯一的方法resolve(Class<?> targetClass)
  • maven-surefire-plugin 插件中配置
 <systemPropertyVariables>
    <spring.profiles.active>${spring.profiles.active}</spring.profiles.active>
</systemPropertyVariables>

實現如下:

1. 標注啟用

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {PetstoreApp.class}, // 我們的 application 名為 PetstoreApp
    webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles(resolver = ProfilesResolver.class)
public abstract class BaseResourceTest {
}

這個類存在的意義就是為了讓其它類別的 ResourceTest 繼承它,并在一次啟動當中運行完所有的集成測試。避免每個 ResourceTest 都初始化啟動 Application,造成運行速度變慢。

注意abstract關鍵字
如果不使用abstract關鍵字,那么maven-surefire-plugin就會拋出如下錯誤:

Tests in error:
BaseResourceTest.initializationError ? No runnable methods

2. 實現自定義類 ProfilesResolver,如下:

import org.springframework.test.context.ActiveProfilesResolver;

public class ProfilesResolver implements ActiveProfilesResolver {
    @Override
    public String[] resolve(Class<?> aClass) {
        String activeProfiles = System.getProperty("spring.profiles.active");
        return new String[] {activeProfiles != null ? activeProfiles : "local"};
    }
}

這里表示我們會從系統變量當中讀取spring.profiles.active,但是這個變量從什么地方來呢?
我首先想到的是 maven 的 profiles 中設置 properties,如下:

<profile>
        <id>local</id>
        <properties>
            <spring.profiles.active>local</spring.profiles.active>
        </properties>
</profile>

如此,當我們在命令行中運行mvn test -Plocal的時候,就表明啟用了 local 這個 profile。相應地,在 maven 的上下文當中,spring.profiles.active變量的值就是local

但是運行測試的時候,我們 ProfilesResolver 中的System.getProperty("spring.profiles.active")返回的始終是null。其實道理很簡單,maven 中定義的 properties 全是給 maven 自己(包含各類插件)用的,它并不會傳遞給應用程序使用。

注意:


properties 中定義的 spring.profiles.active 其實主要是給插件 maven-resources-plugin 使用的,具體請參看備注。


3. 定義systemPropertyVariables

所以我們需要定義systemPropertyVariables,顧名思義,這是系統變量的定義,在應用程序中就可以使用System.getProperty("spring.profiles.active")獲得。

放在哪里合適呢?跑測試的插件中最合適!

所以,我們有如下的配置:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
    <runOrder>alphabetical</runOrder>
    <systemPropertyVariables>
        <spring.profiles.active>${spring.profiles.active}</spring.profiles.active>
    </systemPropertyVariables>
</configuration>
</plugin>

結合上面 properties 的配置,當我們再次運行mvn test -Plocal的時候,就會得到一個名為spring.profiles.active的系統變量,它的值由${spring.profiles.active}決定。此處,就是local。


備注

properties 中 spring.profiles.active 的另外用途
只要 maven 的 properties 中定義了 spring.profiles.active ,運行mvn spring-boot:run -Plocal的時候,spring boot 就會啟用applicaiton-local.yml profile 文件。

為什么會這樣的呢?按常理推斷,應該是spring-boot-maven-plugin的配置項自動讀取了我們設置的 properties spring.profiles.active,但是只要看一眼這個插件的文檔就會發現,除非顯式地在插件的configuration下配置了profiles參數或者手動傳入run.profiles系統變量example,否則插件本身(可以像我一樣掃一眼插件的源碼)并無法感知到底啟用 spring 的哪個 profile!所以這個假設不成立。

答案在bootstrap.yml當中!**
以下是resources/config/bootstrap.yml中的內容

spring:
    application:
        name: petstore
    profiles:
        # The commented value for `active` can be replaced with valid Spring profiles to load.
        # *注意底下這句話*
        # Otherwise, it will be filled in by maven when building the WAR file
        # Either way, it can be overridden by `--spring.profiles.active` value passed in the commandline or `-Dspring.profiles.active` set in `JAVA_OPTS`
        active: #spring.profiles.active#

這里的注釋很有用,明確地告訴我們在構建 WAR 包的時候,maven 會幫我們把#spring.profiles.active#替換成真正的值。

這又是怎么做到的呢?一切歸功于maven-resources-plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>${maven-resources-plugin.version}</version>
    <executions>
        <execution>
            <id>default-resources</id>
            <phase>validate</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>target/classes</outputDirectory>
                <useDefaultDelimiters>false</useDefaultDelimiters>
                <delimiters>
                    <delimiter>#</delimiter> <!-- 看這里 -->
                </delimiters>
                <resources>
                    <resource>
                        <directory>src/main/resources/</directory>
                        <filtering>true</filtering>
                        <includes>
                            <include>**/*.xml</include>
                            <include>**/*.yml</include>
                        </includes>
                    </resource>
                    <resource>
                        <directory>src/main/resources/</directory>
                        <filtering>false</filtering>
                        <excludes>
                            <exclude>**/*.xml</exclude>
                            <exclude>**/*.yml</exclude>
                        </excludes>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

這個插件除了簡單的 copy 功能之外,還能進行 Filtering 操作

Filtering
Variables can be included in your resources. These variables, denoted by the ${...} delimiters, can come from the system properties, your project properties, from your filter resources and from the command line.

大意是說,你可以在 resources 文件定義自己的變量,這些變量可以來自系統屬性、maven 工程屬性,你過濾的 resources 文件和命令行。

說白了,就是在 copy 資源文件的時候,同時幫你把文件中的變量(占位符)替換成真實的值。而這里就是通過<delimiter>#</delimiter>來規定變量格式的!換句話說,在文件中只要是以#開頭和結尾的字符串都會被替換掉(變量有定義的情況下;否則保持原樣)。

這里,由于綁定了生命周期——validate,可以直接運行mvn validate -Plocal這樣的命令進行快速驗證。得到的bootstrap.yml內容如下:

spring:
    application:
        name: petstore
    profiles:
        # The commented value for `active` can be replaced with valid Spring profiles to load.
        # Otherwise, it will be filled in by maven when building the WAR file
        # Either way, it can be overridden by `--spring.profiles.active` value passed in the commandline or `-Dspring.profiles.active` set in `JAVA_OPTS`
        active: dev # 替換成功

回到最開始的疑問,為什么只要 maven 的 properties 中定義了 spring.profiles.active ,運行mvn spring-boot:run -Plocal的時候,就可以spring boot 就會啟用applicaiton-local.yml profile 文件呢?

因為,maven 在運行命令之前已經做了 copy-resources 的操作,那時候就已經把bootstrap.yml中的spring.profiles.active替換成 local 了,所以啟動 springboot application 的時候,它會啟用spring.profiles.active代表的值,此處就是 local,那么啟用的文件自然就是application-local.yml

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容