Profile 可以让 Spring 在不同环境(dev,test,prod 等)下使用不同的配置文件来注册bean,从而提供不同的功能,可以通过激活、指定参数等方式快速切换环境,实现不同配镜的配置隔离。因此,当应用程序在开发环境中运行时,只有某些 bean 可以加载,而在生产环境中时,某些其他 bean 可以加载。假设我们的要求是 Swagger 文档仅适用于 QA 环境,并且禁用所有其他文档,就可以使用配置文件来完成。Spring Boot 使得使用配置文件非常简单。
1、springboot多环境配置(profiles)
我们在开发Spring Boot应用时,通常同一套程序会被应用和安装到几个不同的环境,比如:开发、测试、生产等。其中每个环境的数据库地址、服务器端口等等配置都会不同,如果在为不同环境打包时都要频繁修改配置文件的话,那必将是个非常繁琐且容易发生错误的事。
这就需要多个配置文件来进行不同环境的配置,为了提高开发效率,不在每更换一次环境就改一次配置,我们可以使用多设置几个配置文件来区分不同环境。文件格式可以是 application-{profile}.properties 或 application-{profile}.yml ,但默认是启动主配置文件 application.properties 或 application.yml。我们可以在主配置文件中随时切换成其他环境的配置文件。
- 对于多环境的配置,各种项目构建工具或是框架的基本思路是一致的,通过配置多份不同环境的配置文件,再通过打包命令指定需要打包的内容之后进行区分打包,Spring Boot也不例外,或者说更加简单。
1.1 properties多环境配置
在Spring Boot中多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识,比如:
- application.properties:默认配置
- application-dev.properties:开发环境
- application-test.properties:测试环境
- application-prod.properties:生产环境
至于哪个具体的配置文件会被加载,需要在application.properties文件中通过spring.profiles.active属性来设置,其值对应{profile}值。如:spring.profiles.active=test 就会加载 application-test.properties 配置文件内容。
也可以同时激活多个配置环境,如:spring.profiles.active=hello-world,sender,dev 有三个参数,其中 dev 正好匹配配置中的application-dev.properties 配置文件,所以app启动时,项目会先从application-dev.properties加载配置,再从application.properties配置文件加载配置,如果有重复的配置,则会以application.properties的配置为准。
举例:可以为每个配置文件设置不同的端口:
application-dev.properties
# 开发环境
server.port = 8081
application-test.properties
application-test.properties
#测试环境
server.port = 8082
application-porp.properties
application-porp.properties
#生产环境
server.port = 8083
如果我们需要切换到开发环境,则可以在主配置文件中使用如下指令:
# 本地环境
server.port=8080
# 切换到开发环境
spring.profiles.active=dev
1.2 yml多环境配置
application.yml:
server:
port: 8024
# 切换到dev环境
spring:
profiles:
active: dev
在主配置文件 application.yml 中对需要使用的环境进行选择来切换环境。
1.3 YML多文档块方式
在 xxx.yml 配置文件中,每使用一个 — 分割代表分割成了一个文档块,可以在不同的文档块中进行配置,并在第一个文档块对配置进行切换。下面是配置一个多文档块的 yml 文件示例:
server:
port: 8080
spring:
profiles:
active: test # 切换配置
---
# 开发环境
server:
port: 8081
spring:
profiles: dev
---
# 测试环境
server:
port: 8082
spring:
profiles: test
---
# 生产环境
server:
port: 8083
spring:
profiles: prop
此时需要在第一个多文档块中切换配置,当切换到某一个配置后,该配置(文档块)下的所有配置都能生效。
1.4 IDEA激活profile环境
可以在IDEA 的 Program arguments 进行设置,如 --spring-profiles.active=dev
表示激活了dev环境。
1.5 @Profile注解动态加载配置
可以通过@Profile注解匹配active参数,动态加载内部配置。@profile注解的作用是指定类或方法在特定的 Profile 环境生效,任何@Component或@Configuration注解的类都可以使用@Profile注解。@Profile注解可接受一个或者多个参数。
- @Profile指定组件在哪个环境的情况下才能被注册到容器中,不指定,任何环境下都能注册这个组件。
示例:
@Profile({"tut1","hello-world"})
@Configuration
public class Tut1Config{
@Bean
public Queue hello(){
return new Queue("hello");
}
@Profile("receiver")
@Bean
public Tut1Receiver receiver(){
return new Tut1Receiver();
}
@Profile("sender")
@Bean
public Tut1Sender sender(){
return new Tut1Sender();
}
}
- 当 spring.profiles.active = hello-world,sender 时,该配置类生效,且只有第一个@Bean和第三个@Bean生效。
- 当 spring.profiles.active = hello-world ,则该配置文件生效,第一个@Bean生效。
- 当 spring.profiles.active = sender ,该配置文件未生效,所以下面的@Bean都不会生效。
如此,当我们的项目需要运行在不同环境,特异化配置又比较多,该注解的优势是相当明显的!
2、maven多环境配置(profiles)
在maven构建的项目都存在一个pom.xml的项目对象模型配置文件,用于约束项目(如:jar包管理、构建管理等)。profiles是pom.xml中的一个配置项。我们在开发项目时一般都会区分线上环境和测试环境,这两个环境需要切换以适应不同的环境需求。
正式环境的配置,一般放置于src/main/resources下,而测试环境放置于/src/test/resources下面。profile的主要作用就是区分正式环境和测试环境的配置。
2.1 多环境配置
<profiles>
<profile>
<id>release</id>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
</build>
</profile>
<profile>
<id>test</id>
<build>
<resources>
<resource>
<directory>src/test/resources</directory>
<includes>
<include>config/*.properties</include>
<include>log4j.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
</includes>
<excludes>
<exclude>log4j.xml</exclude>
</excludes>
</resource>
</resources>
</build>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
如上所示,我配置了两个profile,其中release用于正式环境发布,test用于测试环境使用,activation指定了默认配置,示例中默认开启了测试环境,activation->true。如果需要开启某个环境的配置,则把 activation 配置块放入对应的profile节点下。
<activation>
<activeByDefault>true</activeByDefault>
</activation>
本例中测试环境与正式环境只是部分的配置不同,我们需要公用这部分配置,使用到了includes 和 excludes标签,用引入和排除配置文件。
区别构建发布包
构建测试包:
maven package -P test -Dmaven.test.skip=true
构建正式包:
maven package -P release -Dmaven.test.skip=true
2.2 多环境profiles参数切换
在实际开发项目中,常常有几种环境,一般情况下最少有三种环境:开发、测试、正式,各个环境之间的参数也各不相同,于是在环境切换时我们需要修改为各环境需要的参数,就此我们可以通过maven的配置在切换环境时简化这一步骤。
如图所示,dev和test目录分别代码开发和测试环境的配置。
- src/main/resources/dev 目录是开发环境的配置项目
- src/main/resources/test 目录是测试环境的配置项目
在pom.xml定义环境的profile
<profiles>
<profile>
<!-- 本地开发环境 -->
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<project.active>dev</project.active>
</properties>
</profile>
<profile>
<!-- 测试环境 -->
<id>test</id>
<properties>
<project.active>test</project.active>
</properties>
</profile>
<profile>
<!-- 生产环境 -->
<id>prod</id>
<properties>
<profiles.active>prod</profiles.active>
</properties>
</profile>
</profiles>
activeByDefault标签的值为true的话表示为默认的profile,使用mvn install命令起作用的就是默认的 profiles.activation为我们配置激活的profile。下面是打包的配置:
<build>
<finalName>SpringMVC_Spring_Mybatis</finalName>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>message.properties</include>
<include>${project.active}/**</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
<excludes>
<exclude>db.properties</exclude>
<exclude>user.properties</exclude>
<exclude>message.properties</exclude>
<exclude>**/**</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<uriEncoding>UTF-8</uriEncoding>
<path>/</path>
<port>8088</port>
</configuration>
</plugin>
</plugins>
</build>
在工程的${basedir}/src/main/resource 目录下(basedir指的是pom文件所在的目录),<include>
与 <exclude>
是用来圈定和排除某一文件目录下的文件是否是工程资源的,<include>
中指定的为资源文件,<exclude>
中指定的除了这些都为资源文件,如果 <include>
与 <exclude>
划定的范围存在冲突时,以 <exclude>
划定的范围为准。当 <include>
与 <exclude>
指定了相同的文件时,它们是不会被排除的,如message.properties同时被 <include>
与 <exclude>
指定,但它们还是会被编译。
输入maven指令,配置-P后面的test为我们所要激活的profile:
mvn clean install -P test
编译结果如下图:
2.3 pom的多环境properties配置
可以根据pom.xml中三种不同环境分别创建3个配置文件,分别为
application-dev.properties
application-test.properties
application-prod.properties
最后在application.properties中,读取pom.xml中激活的配置。
spring.profiles.active=@profiles.active@
注意: @profiles.active@ 要与pom.xml中的标签保持一致,否则会报错。这样就实现了不同环境配置的切换。
评论区