Profile 是一个 Spring 常用的功能,它可以根据不同的环境配置来加载不同的配置文件,从而实现不同的配置逻辑,
Spring 的 profile#
在 Spring Boot 中,Profile 是通过配置文件来实现的。在不同的环境下,可以加载不同的配置文件,从而实现不同的配置逻辑。具体来说,Spring Boot 支持以下几种配置文件:
application.properties
application.yml
application-{profile}.properties
application-{profile}.yml。
pplication.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>