Profile配置和加载配置文件

6次阅读

共计 3052 个字符,预计需要花费 8 分钟才能阅读完成。

Profile 配置

1.Profile 是什么
很多时候,我们项目在开发环境和生成环境的环境配置是不一样的,例如,数据库配置,在开发的时候,我们一般用测试数据库,而在生产环境的时候,我们是用正式的数据,这时候,我们可以利用 profile 在不同的环境下配置用不同的配置文件或者不同的配置。
spring boot 允许你通过命名约定按照一定的格式 (application-{profile}.properties) 来定义多个配置文件,然后通过在 application.properyies 通过 spring.profiles.active 来具体激活一个或者多个配置文件,如果没有没有指定任何 profile 的配置文件的话,spring boot 默认会启动 application-default.properties。
2. 基于 properties 文件类型

假如有开发、测试、生产三个不同的环境,需要定义三个不同环境下的配置。
你可以另外建立 3 个环境下的配置文件:
applcation.properties
application-dev.properties
application-test.properties
application-prod.properties
然后在 applcation.properties 文件中指定当前的环境:spring.profiles.active=test 这时候读取的就是 application-test.properties 文件。
server.port=8001
# 激活哪个配置文件
spring.profiles.active=dev
spring.profiles.include=prod
可以包含其他的配置文件信息
3. 基于 yml 文件类型
只需要一个 applcation.yml 文件就能搞定,推荐此方式。
spring:
profiles:
active: prod


spring:
profiles: dev

server:
port: 8080


spring:
profiles: test

server:
port: 8081


spring.profiles: prod
spring.profiles.include:
– proddb
– prodmq

server:
port: 8082


spring:
profiles: proddb

db:
name: mysql


spring:
profiles: prodmq

mq:
address: localhost
此时读取的就是 prod 的配置,prod 包含 proddb,prodmq,此时可以读取 proddb,prodmq 下的配置
也可以同时激活三个配置 spring.profiles.active: prod,proddb,prodmq
3. 基于 Java 代码
在 JAVA 配置代码中也可以加不同 Profile 下定义不同的配置文件,@Profile 注解只能组合使用 @Configuration 和 @Component 注解。
@Configuration
@Profile(“prod”)
public class ProductionConfiguration {

// …

}
4. 指定 Profile
不适用配置文件,而是在启动的时候进行指定的写法
4.1 main 方法启动方式:
// 在 IDE Arguments 里面添加
–spring.profiles.active=prod

优先级高于在配置文件里面的激活的
4.2 JVM 启动方式
-Dspring.profiles.active=dev

4.3 插件启动方式
spring-boot:run -Drun.profiles=prod
4.4 jar 运行方式
java -jar xx.jar –spring.profiles.active=prod

除了在配置文件和命令行中指定 Profile,还可以在启动类中写死指定,通过 SpringApplication.setAdditionalProfiles 方法
public void setAdditionalProfiles(String… profiles) {
this.additionalProfiles = new LinkedHashSet<String>(Arrays.asList(profiles));
}

配置文件加载位置
spring boot 启动会扫描以下位置的 application.properties 或者 application.yml 文件作为 Spring boot 的默认配置文件:

file:./config/ – 优先级最高(项目根路径下的 config)

file:./ – 优先级第二 -(项目根路径下)

classpath:/config/ – 优先级第三(项目 resources/config 下)

classpath:/ – 优先级第四(项目 resources 根目录)

重要的规则,跟我们之前学过的不太一样

高优先级配置会覆盖低优先级配置

多个配置文件互补

比如,两个同名文件里面有相同的配置,相同的配置会被高优先级的配置覆盖
A 配置优先级大于 B 配置
server:
port: 8080
B 配置优先级小于 A 配置
server:
port: 8081
context-path: /hanpang
项目启动后访问地址为:http://127.0.0.1:8080/hanpang,这就是所谓的互补

通过配置 spring.config.location 来改变默认配置
java -jar demo-xxx.jar –spring.config.location=C:/application.properties
这对于运维来说非常方便,在不破坏原配置情况下轻松修改少量配置就可以达到想要的效果

外部配置加载顺序
来自于网路,个人没有进行相关的测试
SpringBoot 也可以从以下位置加载配置:优先级从高到低;高优先级的配置覆盖低优先级的配置,所有的配置会形成互补配置。

命令行参数

所有的配置都可以在命令行上进行指定;

多个配置用空格分开;– 配置项 = 值
java -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar
–server.port=8087 –server.context-path=/abc

来自 java:comp/env 的 JNDI 属性
Java 系统属性(System.getProperties())
操作系统环境变量
RandomValuePropertySource 配置的 random.* 属性值
jar 包外部的 application-{profile}.properties 或 application.yml(带 spring.profile)配置文件
jar 包内部的 application-{profile}.properties 或 application.yml(带 spring.profile)配置文件
.jar 包外部的 application.properties 或 application.yml(不带 spring.profile)配置文件

jar 包内部的 application.properties 或 application.yml(不带 spring.profile)配置文件
由 jar 包外向 jar 包内进行寻找,优先加载待 profile 的,再加载不带 profile 的。

@Configuration 注解类上的 @PropertySource
通过 SpringApplication.setDefaultProperties 指定的默认属性

加载配置文件方式参考官网地址

正文完
 0