Update 23.3. Application property files.md

master
qibaoguang 2015-02-08 21:56:20 +08:00
parent 3f144afaee
commit b9579a6bcb
1 changed files with 22 additions and 0 deletions

View File

@ -0,0 +1,22 @@
### 23.3. Application属性文件
SpringApplication将从以下位置加载application.properties文件并把它们添加到Spring Environment中
1. 当前目录下的一个/config子目录
2. 当前目录
3. 一个classpath下的/config包
4. classpath根路径root
这个列表是按优先级排序的(列表中位置高的将覆盖位置低的)。
**注**你可以使用YAML'.yml')文件替代'.properties'。
如果不喜欢将application.properties作为配置文件名你可以通过指定spring.config.name环境属性来切换其他的名称。你也可以使用spring.config.location环境属性来引用一个明确的路径目录位置或文件路径列表以逗号分割
```shell
$ java -jar myproject.jar --spring.config.name=myproject
//or
$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
```
如果spring.config.location包含目录相对于文件那它们应该以/结尾在加载前spring.config.name产生的名称将被追加到后面。不管spring.config.location是什么值默认的搜索路径classpath:,classpath:/config,file:,file:config/总会被使用。以这种方式你可以在application.properties中为应用设置默认值然后在运行的时候使用不同的文件覆盖它同时保留默认配置。
**注**如果你使用环境变量而不是系统配置大多数操作系统不允许以句号分割period-separated的key名称但你可以使用下划线underscores代替比如使用SPRING_CONFIG_NAME代替spring.config.name。如果你的应用运行在一个容器中那么JNDI属性java:comp/env或servlet上下文初始化参数可以用来取代环境变量或系统属性当然也可以使用环境变量或系统属性。