1 配置文件
1.1 默认配置文件
默认配置文件:
application.properties或application.yml
1.2 多个配置文件
application.propertiesapplication.ymlapplication-dev.ymlapplication-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@Datapublic 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.properties2019-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注解类上的@PropertySourcek. 通过SpringApplication.setDefaultProperties指定的默认属性
