SpringApplication 将从以下位置加载 application.properties 文件,并把它们添加到Spring Environment 中:
- 当前目录下的 /config 子目录。
- 当前目录。
- classpath下的 /config 包。
- classpath根路径(root)。
该列表是按优先级排序的(列表中位置高的路径下定义的属性将覆盖位置低的)。
Profile
Environment有个默认profiles集合(默认情况为 [default] ),在没有设置激活的profiles时会被使用(例如,如果没有明确指定激活的profiles, application-default.properties 中的属性会被加载)。
定义当前项目的profile,SpringApplication会自动加载application-{profile}.properties文件中的属性,而且该文件中的属性会覆盖 application.properties 的属性
spring.profiles.active=prod
属性占位符
当使用 application.properties 定义的属性时,Spring会先通过已经存在的 Environment 查找该属性,所以你可以引用事先定义的值(比如,系统属性):
app.name=MyApp
app.description=${app.name} is a Spring Boot application
配置随机值
需要的时候你还可以配置随机值(密钥或测试用例),
my.secret=${random.value}
my.number=${random.int}
my.bignumber=${random.long}
my.number.less.than.ten=${random.int(10)}
my.number.in.range=${random.int[1024,65536]}