SpringBoot(三):SpringBoot 属性配置文件详解

      版权声明:本文为博主原创文章,转载请注明出处http://blog.csdn.net/saytime          https://blog.csdn.net/saytime/article/details/74781708        </div>
        <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-f57960eb32.css">
                          <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-f57960eb32.css">
      <div class="htmledit_views" id="content_views">

SpringBoot 配置文件默认为application.properties,但是本章节主要讲解yaml文件配置,因为现在的趋势是使用yaml,它是类似于标准通用标记语言的子集XML的数据描述语言,语法比XML简单很多。

一、自定义属性与加载

我们把之前项目中的配置文件application.properties改成application.yml


 
 
  1. test:
  2. user:
  3. username : zhangsan
  4. age : 18
  5. toString: the age of ${test. user .username} is ${test. user .age}

属性类


 
 
  1. package cn.saytime.bean;
  2. import org.springframework.beans. factory. annotation.Value;
  3. import org.springframework.stereotype.Component;
  4. @Component
  5. public class PropertiesConfig {
  6. @Value ( "${test.user.username}" )
  7. private String username;
  8. @Value ( "${test.user.age}" )
  9. private String age;
  10. @Value ( "${test.user.toString}" )
  11. private String toString;
  12. // ... Getter Setter
  13. }

测试Controller


 
 
  1. package cn.saytime.web;
  2. import cn.saytime.bean.PropertiesConfig;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestMethod;
  6. import org.springframework.web.bind.annotation.RestController;
  7. @RestController
  8. public class TestController {
  9. @Autowired
  10. private PropertiesConfig propertiesConfig;
  11. @RequestMapping(value = "test" , method = RequestMethod.GET)
  12. public String test(){
  13. // return propertiesConfig.getUsername() + ":" + propertiesConfig.getAge();
  14. return propertiesConfig.getToString();
  15. }
  16. }

访问 http://127.0.0.1/test

这里写图片描述

二、自定义属性注入bean

将上面application.yml文件中的test.user注入到User对象里,注意这里的prefix指定的是test.user,对应配置文件中的结构


 
 
  1. @Component
  2. @ConfigurationProperties(prefix = "test.user")
  3. public class User {
  4. private String username;
  5. private int age;
  6. public String getUsername() {
  7. return username;
  8. }
  9. // Getter Setter
  10. }

 
 
  1. @ RestController
  2. public class TestController {
  3. @ Autowired
  4. private User user;
  5. @ RequestMapping (value = "test2" , method = RequestMethod . GET )
  6. public String test2(){
  7. return user.getUsername() + ":" + user.getAge();
  8. }
  9. }

访问 http://localhost:8080/test2 ==> zhangsan : 18

三、自定义额外的配置文件

比如我们不想把有些配置配置在application.yml/properties中。新建其他配置文件。这里比如新建test.yml文件。配置内容如上面的application.yml一样。那么我们注入对象时,应该这么写


 
 
  1. @Configuration
  2. @PropertySource (value = "classpath:test.yml" )
  3. @ConfigurationProperties (prefix = "test.user" )

四、多个环境配置文件

在现实的开发环境中,我们需要不同的配置环境;格式为application-{profile}.properties,其中{profile}对应你的环境标识,比如:

  • application-test.properties:测试环境
  • application-dev.properties:开发环境
  • application-prod.properties:生产环境

怎么使用?只需要我们在application.yml中加:


 
 
  1. spring:
  2. profiles:
  3. active: dev

因为主入口都是application.yml,这里指定配置文件为dev,即启用application-dev.yml文件

其中application-dev.yml:


 
 
  1. server:
  2. port: 8888

启动工程,发现程序的端口不再是8080,而是8888,表示开发环境配置成功。

六、官方支持默认配置文件属性

http://docs.spring.io/spring-boot/docs/1.5.4.RELEASE/reference/htmlsingle/#common-application-properties

七、属性加载优先级


 
 
  1. 1. @TestPropertySource 注解
  2. 2. 命令行参数
  3. 3. Java系统属性(System.getProperties())
  4. 4. 操作系统环境变量
  5. 5. 只有在random.*里包含的属性会产生一个RandomValuePropertySource
  6. 6. 在打包的jar外的应用程序配置文件(application-{profile}.properties,包含YAML和profile变量)
  7. 7. 在打包的jar内的应用程序配置文件(application-{profile}.properties,包含YAML和profile变量)
  8. 8.@Configuration类上的 @PropertySource注解
  9. 9. 默认属性(使用SpringApplication.setDefaultProperties指定)

也就是说比如我配置文件配置了一个name=zhangsan,然后将项目打成jar,运行的时候,如果我们使用 java -jar app.jar --name="Spring",那么注入的进去的就是Spring,优先级高

八、配置文件优先级

查看SpringBoot官方文档,可以发现

这里写图片描述

翻译:

  1. 当前目录下的一个/config子目录
  2. 当前目录
  3. 一个classpath下的/config包
  4. classpath根路径(root)

也就是说:如果我们有多个配置文件,比如 src/main/resource/application.yml


 
 
  1. test:
  2. user:
  3. username : zhangsan
  4. age : 18
  5. toString: the age of ${test.user.username} is ${test.user.age}
  6. name: SpringBoot-root
  7. test2: ${test1}-root
  8. test3: SpringCloud-root
  9. server:
  10. port: 8080

src/main/resource/config/application.yml


 
 
  1. test:
  2. user:
  3. username: lisi-config
  4. name: SpringBoot-config
  5. test1: ${name}-config
  6. test4: ${test3}-config
  7. server:
  8. port: 9090

根据优先级,可以得到能够加载到SpringBoot应用的属性为:


 
 
  1. test:
  2. user:
  3. username : lisi
  4. age : 18
  5. toString: the age of lisi is 18
  6. name: SpringBoot-config
  7. test1: SpringBoot-config-config
  8. test2: SpringBoot-config-config-root
  9. test3: SpringCloud-root
  10. test4: SpringCloud-root-config
  11. server:
  12. port: 9090

如果你能够得到上面的结果,表示你已经懂了。

注意优先级高的配置文件中存在和优先级低的配置文件相同属性时,取优先级高配置文件的,不冲突的时候,优先级低的配置文件属性同样会被加载,而不是只加载优先级高的配置文件属性。

九、简单总结

1、普通自定义属性,使用@Value("${xxx}")注入 2、注入对象,使用@ConfigurationProperties(prefix="test.user")




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值