Profile 是一個 Spring 常用的功能,它可以根據不同的環境配置來加載不同的配置文件,從而實現不同的配置邏輯,
Spring 的 profile#
在 Spring Boot 中,Profile 是通過配置文件來實現的。在不同的環境下,可以加載不同的配置文件,從而實現不同的配置邏輯。具體來說,Spring Boot 支持以下幾種配置文件:
application.properties
application.yml
application-{profile}.properties
application-{profile}.yml。
application.properties 和 application.yml 是通用的配置文件,它們在所有的環境下都會被加載。而 application-{profile}.properties 和 application-{profile}.yml 則是根據不同的 Profile 來加載的配置文件。當應用程序啟動時,Spring Boot 會根據目前的環境變量來決定加載哪個配置文件。例如,如果目前環境變量為 dev,則會加載 application-dev.properties 或 application-dev.yml 文件。
使用#
在實際開發中,Profile 可以用來實現以下幾種功能:
- 區分不同的環境,例如開發環境、測試環境和生產環境。
- 配置不同的資料庫連接信息,例如開發環境使用本地 MySQL 資料庫,測試環境使用遠程 MySQL 資料庫,生產環境使用阿里雲 RDS 資料庫。
- 配置不同的日誌級別,例如開發環境使用 DEBUG 級別,測試環境使用 INFO 級別,生產環境使用 WARN 級別。
透過配置文件進行切換#
在項目的 resources 目錄下創建三個不同的配置文件,application-prod.properties,application-test.properties,application-dev.properties;項目的配置文件中,需要配置當前的 Profile。有以下兩種方法:
- 在 application.properties 文件中配置。
spring.profiles.active=dev
- 在啟動命令中配置。
java -jar myproject.jar --spring.profiles.active=dev
maven 的 profile#
maven 的 profile 的原理是定義一套 profiles 集合,在不同的 profile 定義不同的屬性值集合,通過對 profile 的選擇,來使不同的屬性集合生效。
在 pom.xml 文件中添加不同的 profile:
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!-- default Spring profiles -->
<profiles.active>dev</profiles.active>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<!-- default Spring profiles -->
<profiles.active>prod</profiles.active>
</properties>
</profile>
</profiles>
再在 pom.xml 進行如下配置,兩者搭配,則可以指定 src/main/resources/${profiles.active} 的那個 文件夾 下的配置文件會被使用。
<build>
<resources>
<!-- 所有公共資源文件 -->
<resource>
<directory>src/main/resources</directory>
</resource>
<!-- 不同環境的資源文件 -->
<resource>
<directory>src/main/resources/${profiles.active}</directory>
<!-- Filter 是 maven 的 resource 插件提供的功能,作用是用環境變量、pom 文件裡定義的屬性和指定外部配置文件裡的屬性替換屬性文件(*.properties)裡的佔位符(${}),但上述的 maven-resources-plugin 自定義了通配符為 @@,因此是將 src/main/resources/${profiles.active} 裡的文件中用 @@ 佔位的參數進行替換。 -->
<filtering>true</filtering>
<includes>
<include>/*.*</include>
</includes>
</resource>
</resources>
</build>
就可以在 idea 中切換 profile 了
該配置添加了兩個 profile,一個 dev,一個 prod,activeByDefault 表示默認選擇。
- Maven 啟動,指定 mvn 中的 Profile 通過 -P,如 mvn spring-boot -Pdev
- Maven 打包,指定 mvn 中的 Profile 通過 -P,如 mvn package -Pdev -DskipTests
在構建 WAR 包的時候會經過資源文件處理階段,maven-resources-plugin 則用來處理資源文件。在 pom.xml 中的 build 節點下配置如下:
<plugins>
<plugin>
<!-- 跟本文的 profile 沒關係 但還是一起寫了 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<!-- 編譯插件,此處用來設置 jdk 的版本,否則默認的版本很低 -->
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<testSource>${java.version}</testSource>
<testTarget>${java.version}</testTarget>
</configuration>
<version>${maven-compiler-plugin.version}</version>
</plugin>
<!-- 資源文件處理插件,必須配置 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>${maven-resources-plugin.version}</version>
<configuration>
<delimiters>
<delimiter>@</delimiter>
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
</plugin>
</plugins>
擴展#
maven 安裝包中 conf/setting.xml 下的配置文件中也有 profiles。
我們知道 pom 中也是有 profiles 這個屬性的,那麼它們的區別是 settings.xml 總的 profile 和 pom 中的 profile 的區別是,settings.xml 中的 profile 只有 activation,repositories,plugineReositories,properties 四個子元素。通常是用來配置倉庫信息的。
<profiles>
<profile>
<!-- profile 的唯一標識 -->
<id>prod</id>
<!-- 自動觸發 profile 的條件邏輯 -->
<activation />
<!-- 擴展屬性列表 -->
<properties />
<!-- 遠程倉庫列表 -->
<repositories />
<!-- 插件倉庫列表 -->
<pluginRepositories />
</profile>
</profiles>
<!-- 可以配置多個 profile。一般是做全局配置 -->
<activeProfiles>
<!-- make the profile active all the time -->
<activeProfile>ang-thirdparty-nexus</activeProfile>
<activeProfile>ang-central-nexus</activeProfile>
<activeProfile>aliyun-nexus</activeProfile>
<activeProfile>zcg-nexus</activeProfile>
</activeProfiles>