1 配置文件
1.1 默认配置文件
默认配置文件:
application.properties
或
application.yml
1.2 多个配置文件
application.properties
application.yml
application-dev.yml
application-local.yml
根据配置决定哪个生效:spring.profiles.active=local
1.3 配置文件中属性读取
1.3.1 会取生效的配置文件找master前缀的配置
@ConfigurationProperties(prefix = “master”)
@Component
@Data
@ConfigurationProperties(prefix = "master")
public class MasterByDefaultProperties {
private String name;
private String sex;
private String subject;
}
1.3.2 会取生效的配置文件找“master.name”的配置
@Value(“${master.name}”)
@Component
@Data
public class MasterByProperties {
@Value("${master.name}")
private String name;
@Value("${master.sex}")
private String sex;
@Value("${master.subject}")
private String subject;
}
1.3.3 @PropertySource和@Value联合使用,取@PropertySource指定配置文件找对应的配置
@Data
@Component
@PropertySource(value = "classpath:application.yml")
public class MasterByDefaultYml {
@Value("${master.name}")
private String name;
@Value("${master.sex}")
private String sex;
@Value("${master.subject}")
private String subject;
}
1.4 多个配置文件中相同属性的读取
读取方式跟上边一样,但是重名的属性,则根据优先级,优先级高的覆盖低的;
生效的主配置>生效的副配置 properties>yml
# 生效的application-local.yml>application.properties
2019-06-11 16:43:16.414 INFO 5490 --- [main] com.wxx.modules.st.StApplication : The following profiles are active: local
2 配置方式优先级:
a. 命令行参数
b. 来自java:comp/env的JNDI属性
c. Java系统属性(System.getProperties())
d. 操作系统环境变量
e. RandomValuePropertySource配置的random.*属性值
f. jar外部的application-{profile}.properties或application.yml(带spring.profile)配置文件
g. jar内部的application-{profile}.properties或application.yml(带spring.profile)配置文件
h. jar外部的application.properties或application.yml(不带spring.profile)配置文件
i. jar内部的application.properties或application.yml(不带spring.profile)配置文件
j. @Configuration注解类上的@PropertySource
k. 通过SpringApplication.setDefaultProperties指定的默认属性