本篇博客是看了别人的博客然后根据自己的理解整理了出来的笔记,发出来大家交流学习,如果有不足的地方请大家指正,大家也可以直接去源博客那里看,源博客写的真的很好 。
源博客地址:http://www.ibm.com/developerworks/cn/java/j-lo-spring-boot/#ibm-pcon
3. Spring Boot 所提供的配置优先级顺序比较复杂。
按照优先级从高到低的顺序,具体的列表如下所示:
- 命令行参数。
- 通过 System.getProperties() 获取的 Java 系统参数。
- 操作系统环境变量。
- 从 java:comp/env 得到的 JNDI 属性。
- 通过 RandomValuePropertySource 生成的“random.*”属性。
- 应用 Jar 文件之外的属性文件。
- 应用 Jar 文件内部的属性文件。
- 在应用配置 Java 类(包含“@Configuration”注解的 Java 类)中通过“@PropertySource”注解声明的属性文件。
- 通过“SpringApplication.setDefaultProperties”声明的默认属性。
说明:
1> Spring Boot 的这个配置优先级看似复杂,其实是很合理的。比如命令行参数的优先级被设置为最高。这样的好处是可以在测试或生产环境中快速地修改配置参数值,而不需要重新打包和部署应用。
清单 3. 使用 RandomValuePropertySource 生成的配置属性
user.id=${random.value}
user.count=${random.int}
user.max=${random.long}
user.number=${random.int(100)}
user.range=${random.int[100, 1000]}
1> Spring Boot 提供的 SpringApplication 类会搜索并加载 application.properties 文件来获取配置属性值。
2> SpringApplication 类会在下面位置搜索该文件。
- 。当前目录的“/config”子目录。
- 。当前目录。
- 。classpath 中的“/config”包。
- 。classpath
注意:上面的顺序也表示了该位置上包含的属性文件的优先级。优先级按照从高到低的顺序排列。
1> 可以通过“spring.config.name”配置属性来指定不同的属性文件名称。
2> 也可以通过“spring.config.location”来添加额外的属性文件的搜索路径。
3> 如果应用中包含多个 profile,可以为每个 profile 定义各自的属性文件,按照“application-{profile}”来命名。
@RestController
@EnableAutoConfiguration
public class Application {
@Value("${name}")
private String name;
@RequestMapping("/")
String home() {
return String.format("Hello %s!", name);
}
}
spring:
profiles: development
db:
url: jdbc:hsqldb:file:testdb
username: sa
password:
---
spring:
profiles: test
db:
url: jdbc:mysql://localhost/test
username: test
password: test
清单 6. 使用 YAML 文件的 Java 类
@Component
@ConfigurationProperties(prefix="db")
public class DBSettings {
private String url;
private String username;
private String password;
}
3. Spring Boot 所提供的配置优先级顺序比较复杂。
按照优先级从高到低的顺序,具体的列表如下所示:
- 命令行参数。
- 通过 System.getProperties() 获取的 Java 系统参数。
- 操作系统环境变量。
- 从 java:comp/env 得到的 JNDI 属性。
- 通过 RandomValuePropertySource 生成的“random.*”属性。
- 应用 Jar 文件之外的属性文件。
- 应用 Jar 文件内部的属性文件。
- 在应用配置 Java 类(包含“@Configuration”注解的 Java 类)中通过“@PropertySource”注解声明的属性文件。
- 通过“SpringApplication.setDefaultProperties”声明的默认属性。
说明:
1> Spring Boot 的这个配置优先级看似复杂,其实是很合理的。比如命令行参数的优先级被设置为最高。这样的好处是可以在测试或生产环境中快速地修改配置参数值,而不需要重新打包和部署应用。
清单 3. 使用 RandomValuePropertySource 生成的配置属性
user.id=${random.value}
user.count=${random.int}
user.max=${random.long}
user.number=${random.int(100)}
user.range=${random.int[100, 1000]}
1> Spring Boot 提供的 SpringApplication 类会搜索并加载 application.properties 文件来获取配置属性值。
2> SpringApplication 类会在下面位置搜索该文件。
- 。当前目录的“/config”子目录。
- 。当前目录。
- 。classpath 中的“/config”包。
- 。classpath
注意:上面的顺序也表示了该位置上包含的属性文件的优先级。优先级按照从高到低的顺序排列。
1> 可以通过“spring.config.name”配置属性来指定不同的属性文件名称。
2> 也可以通过“spring.config.location”来添加额外的属性文件的搜索路径。
3> 如果应用中包含多个 profile,可以为每个 profile 定义各自的属性文件,按照“application-{profile}”来命名。
@RestController
@EnableAutoConfiguration
public class Application {
@Value("${name}")
private String name;
@RequestMapping("/")
String home() {
return String.format("Hello %s!", name);
}
}
spring:
profiles: development
db:
url: jdbc:hsqldb:file:testdb
username: sa
password:
---
spring:
profiles: test
db:
url: jdbc:mysql://localhost/test
username: test
password: test
清单 6. 使用 YAML 文件的 Java 类
@Component
@ConfigurationProperties(prefix="db")
public class DBSettings {
private String url;
private String username;
private String password;
}