目录
简介
一, 配置样例
二, 如何注入配置
1. 缺省配置文件
2. 使用注解
3. 启动参数
还有..
三, 如何读取配置
@Value 注解
Environment 接口
@ConfigurationProperties 注解
四, 不同环境中的配置
1. 区别开发, 测试, 发布环境
2. 声明多配置文件
参考文档
简介
在早前的博客中曾经写过 Spring 程序通过 Bean 映射实现配置信息的读取.
在 SpringBoot 框架中读取配置的方式变得非常多样, 这导致读者在搜寻资料时反而容易迷糊.
到底, SpringBoot 是按什么顺序加载配置?
相应的, 我们该选择什么样的方式去读取?
一, 配置样例
先看一个例子:
- @Compoment
- public class BuildConfig{
- @Value("${buildinfo.version")
- private String version;
- ...
- }
代码中,@Component 将 BuildConfig 注册为 Bean ,
接下来使用 @Value 注解, 将 配置中的 buildinfo.version 键映射到了 version 字段上.
我们都知道, 通过 application.properties 可以方便的配置一些属性.
属性的值是支持变量替换的, 如下:
- myName=Lilei
- myDesc=${
- myName
- } is a good man
这点, 是由 SpringBoot 自动生成的 PropertyPlaceholderConfigurer 对象实现的.
除了 上面所说 application.properties 之外, 还有什么途径?
下面介绍如何注入配置
二, 如何注入配置
1. 缺省配置文件
类路径中 application.properties(YAML) 是默认的配置文件.
此外如果启动应用时, 当前目录中存在同名的配置文件, 则以此优先.
在此规则之下, SpringBoot 还能识别不同 profile 下的配置, 这将在后面篇幅中介绍.
2. 使用注解
@PropertySource
可指定属性配置文件的位置,
样例代码:
- @Configuration
- @PropertySource("classpath:/com/myco/App.properties")
- public class AppConfig {
- @Autowired
- Environment env;
- @Bean
- public TestBean testBean() {
- TestBean testBean = new TestBean();
- testBean.setName(env.getProperty("testbean.name"));
- return testBean;
- }
- }
- @TestPropertySource
与 @PropertySource 类似, 该注解用于指定测试环境中的属性文件, 其优先级高于 @PropertySource.
3. 启动参数
以下的命令以指定参数启动 SpringBoot 应用
java -jar application.jar --server.port=9000
server.port 值将被注入为环境属性值.
而以下的命令还可以指定 配置文件的位置
java -jar application.jar --spring.config.location=/etc/xxx.properties
这个 spring.config.location 就是指的配置文件位置,
默认情况下, SpringBoot 会从下面几路径找到配置文件:
路径 |
---|
file:./config/ |
file:./ |
classpath:/config/ |
classpath:/ |
还有..
SpringBoot 注入配置的方式其实非常多, 完整顺序如下表:
优先级 | 配置 |
---|---|
1 | @TestPropertySource 注解 |
2 | @SpringBootTest 注解 |
3 | 命令行参数 |
4 | SPRING_APPLICATION_JSON 属性值 (或环境变量) |
5 | Servlet 相关参数 |
6 | JNDI 属性 |
7 | Java 系统属性 (System.getProperties()) |
8 | 操作系统环境变量 |
9 | RandomValuePropertySource 随机属性 |
10 | Jar 包外部 application-{profile}.properties |
11 | Jar 包内部 application-{profile}.properties |
12 | Jar 包外部 application.properties |
13 | Jar 包内部 application.properties |
14 | @PropertySource 注解 |
15 | SpringApplication 默认值 |
三, 如何读取配置
@Value 注解
如以下的实现:
- @Configuration
- public class AppConfig {
- @Value("${API.log.enabled:false}")
- private boolean apiLogEnabled;
除了类型自动转换之外, 通过: false 后缀可以指定默认值.
Environment 接口
Environment 是一个类似 Properties 的接口, 用来获取属性非常方便.
- @Configuration
- public class AppConfig {
- @Autowired
- private Environment environment;
- public String getApplicationId() {
- return this.environment.getProperty("application.id");
- }
- }
@ConfigurationProperties 注解
该注解一般用作前缀匹配, 下面的代码摘自 MongoDB
- @ConfigurationProperties(prefix = "spring.data.MongoDB")
- public class MongoProperties {
- /**
- * Mongo server host.
- */
- private String host;
- /**
- * Mongo server port.
- */
- private Integer port = null;
- /**
- * Database name.
- */
- private String database;
相应的 MongoDB 配置信息如:
- spring.data.MongoDB.host=127.0.0.1
- spring.data.MongoDB.port=27017
- spring.data.MongoDB.database=xxx
四, 不同环境中的配置
Spring 提供了 Profile 机制用于管理不同环境的配置.
配置内容可以是 Java Config(对应 @Component 或 @Configuration), 也可以是配置文件.
如:
- @Configuration
- @Profile("prod")
- public class ProdConfiguration {
- // ...
- }
通过 @Profile 注解可将代码配置关联到某个配置环境
在具体应用中, Profile 的用途通常有二:
1. 区别开发, 测试, 发布环境
对于 dev,prod,test 分别做不同的配置
- //for dev
- application-dev.properties
- //for prod
- application-prod.properties
- //for test
- application-test.properties
可以在 application.properties 指定启用的环境:
spring.profiles.active=dev
也可以通过命令行指定:
java -jar App.jar --spring.profiles.active=prod
2. 声明多配置文件
当内容过多时, 可以将配置信息进行拆分, 如下:
- application-MongoDB.properties
- spring.data.MongoDB.host=127.0.0.1
- spring.data.MongoDB.port=27017
- spring.data.MongoDB.username=xxx
- spring.data.MongoDB.password=xxx
- spring.data.MongoDB.database=xxx
- application-mail.properties
- spring.mail.host=xxx
- spring.mail.username=xxx
- spring.mail.password=xxx
- spring.mail.from=xxx
- spring.mail.to=xxx
- spring.mail.cc=xxx
在主配置文件指定包含关系:
- application.properties
- spring.profiles.include=MongoDB,mail
参考文档
- https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.HTML
- https://docs.spring.io/spring-boot/docs/current/reference/HTML/boot-features-profiles.HTML
来源: https://www.cnblogs.com/littleatp/p/9703855.html