当前位置:首页 » 《休闲阅读》 » 正文

SpringBoot2.X基础教程:配置文件详解_java程序鱼的博客

22 人参与  2022年02月04日 12:00  分类 : 《休闲阅读》  评论

点击全文阅读


💂 个人主页: Java程序鱼

💬 如果文章对你有帮助,欢迎关注、点赞、收藏(一键三连)和订阅专栏

👤 微信号:hzy1014211086,如果你正在学习Spring Boot,可以加入我们的Spring技术交流群,共同成长


序号内容
1面试题专栏
2Redis专栏
3SpringBoot专栏
3SpringBoot专栏更新计划

文章目录

  • 一、简介
  • 二、properties
    • 自定义属性
    • 参数间引用
    • 自定义配置文件地址
    • 外部配置
    • 优先级
    • 多环境配置
  • 三、YAML
  • 四、源码


一、简介

Spring Boot 的核心是自动配置(或者叫默认配置),通过自动配置大大减少Spring项目的配置编写。但是在实际开发中,我们仍然需要根据需求来适当修改某些必要的参数配置,这时Spring Boot提供了两种格式的配置方便开发者进行修改。

  • applicaiton*.properties
  • application*.yml(或者application*.yaml)

二、properties

自定义属性

我们除了可以在 Spring Boot 的配置文件中设置各个 Starter 模块中预定义的配置属性,也可以在配置文件中定义一些我们需要的自定义属性。比如在 application.properties 中添加:

blog.id=1
blog.title=SpringBoot
blog.author=fish

然后,在应用中我们可以通过@Value注解来加载这些自定义的参数,比如:

@Data
@Component
public class Blog {
    @Value("${blog.id}")
    private Long id;
    @Value("${blog.title}")
    private String title;
    @Value("${blog.author}")
    private String author;
}

大家有没有发现一个问题,如果一个对象属性太多,一个一个绑定到属性字段上是不是太麻烦,如何解决呢?

@Data
@Component
@ConfigurationProperties(prefix = "blog")
public class Blog2 {
  private Long id;
  private String title;
  private String author;
}

这里配置完还需要在 Spring Boot 启动类加上@EnableConfigurationProperties 并指明要加载哪个bean

@EnableConfigurationProperties({Blog2.class})
@SpringBootApplication
public class Chapter2Application {

	public static void main(String[] args) {
		SpringApplication.run(Chapter2Application.class, args);
	}

}

参数间引用

在application.properties中的各个参数之间也可以直接引用来使用,就像下面的设置:

blog.id=1
blog.title=SpringBoot
blog.author=fish
blog.desc=${blog.author} wrote an article about ${blog.title}
@RestController
public class TestController {
  @Autowired
  private Blog2 blog2;

  @RequestMapping("/test2")
  public String test2() {
    return blog2.toString();
  }
}

@Data
@Component
@ConfigurationProperties(prefix = "blog")
public class Blog2 {
  private Long id;
  private String title;
  private String author;
  private String desc;
}

在这里插入图片描述

自定义配置文件地址

有时候我们不希望把所有配置都放在 application.properties 里面,这时候我们可以另外定义一个,这里我明取名为 blog.properties ,路径跟也放在 src/main/resources 下面。

custom.blog.id=2
custom.blog.title=SpringBoot custom config
custom.blog.author=fish
custom.blog.desc=${blog.author} wrote an article about ${blog.title}
@RestController
public class TestController {
  @Autowired
  private CustomBlog customBlog;

  @RequestMapping("/test3")
  public String test3() {
    return customBlog.toString();
  }
}

@Data
@Component
@ConfigurationProperties(prefix = "custom.blog")
@PropertySource("classpath:blog.properties")
public class CustomBlog {
  private Long id;
  private String title;
  private String author;
  private String desc;
}

在这里插入图片描述

外部配置

SpringBoot 可以使用命令 java -jar 命令来启动 SpringBoot 应用,该命令除了启动应用之外,还可以在命令行中来指定应用的参数,比如:java -jar 项目名.jar --server.port=8081,直接以命令行的方式,来设置 server.port 属性,另启动应用的端口设为 8081。

可以看出,命令行中连续的两个减号–就是对application.properties中的属性值进行赋值的标识。所以java -jar 项目名.jar --server.port=8081 等价于在 application.properties 中添加属性server.port=8081。
如果你怕命令行有风险,可以使用 SpringApplication.setAddCommandLineProperties(false) 禁用它。

实际上,Spring Boot 应用程序有多种设置途径,Spring Boot 能从多重属性源获得属性,包括如下几种:

  • 在您的主目录(当 devtools 被激活,则为 ~/.spring-boot-devtools.properties)中的 Devtools 全局设置属性。
  • 在测试中使用到的 @TestPropertySource 注解。
  • 在测试中使用到的 properties 属性,可以是 @SpringBootTest 和用于测试应用程序某部分的测试注解。
  • 命令行参数。
  • 来自 SPRING_APPLICATION_JSON 的属性(嵌入在环境变量或者系统属性【system propert】中的内联 JSON)。
  • ServletConfig 初始化参数。
  • ServletContext 初始化参数。
  • 来自 java:comp/env 的 JNDI 属性。
  • Java 系统属性(System.getProperties())。
  • 操作系统环境变量。
  • 只有 random.* 属性的 RandomValuePropertySource。
  • 在已打包的 jar 外部的指定 profile 的应用属性文件(application-{profile}.properties 和 YAML 变量)。
  • 在已打包的 jar 内部的指定 profile 的应用属性文件(application-{profile}.properties 和 YAML 变量)。
  • 在已打包的 jar 外部的应用属性文件(application.properties 和 YAML 变量)。
  • 在已打包的 jar 内部的应用属性文件(application.properties 和 YAML 变量)。
  • 在 @Configuration 类上的 @PropertySource 注解。
  • 默认属性(使用 SpringApplication.setDefaultProperties 指定)。

这里列表按组优先级排序,也就是说,任何在高优先级属性源里设置的属性都会覆盖低优先级的相同属性,列如我们上面提到的命令行属性就覆盖了application.properties 的属性。

在我平时工作中,用的比较多的方式是YAML、application.properties、jar命令行,其他的都很少用

优先级

我们创建一个 Spring Boot 工程时,默认 resources 目录下就有一个 application.properties 文件,可以在 application.properties 文件中进行项目配置,但是这个文件并非唯一的配置文件,在 Spring Boot 中,一共有 4 个地方可以存放 application.properties 文件。

  • 当前项目根目录下的 config 目录下
  • 当前项目的根目录下
  • resources 目录下的 config 目录下
  • resources 目录下

加载优先级:当前项目根目录下的 config 目录下 > 当前项目的根目录下 > resources 目录下的 config 目录下 > resources 目录下

也就是说,src/main/resources/config 下 application.properties 覆盖 src/main/resources 下application.properties 中相同的属性

多环境配置

当应用程序需要部署到不同运行环境时,一些配置细节通常会有所不同,例如日志级别、数据源、各种密码等等,如果按照以前的做法,就是每次发布的时候替换掉配置文件,这样太麻烦了,如何解决呢?

在 Spring Boot 中多环境配置文件名需要满足 application-{profile}.properties 的格式,其中{profile}对应你的环境标识,比如:

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

想要使用对应的环境,只需要在application.properties中使用spring.profiles.active属性来设置,值对应上面提到的{profile},例如:spring.profiles.active=dev 就会加载 application-dev.properties 配置文件内容。

至于哪个具体的配置文件会被加载,需要在application.properties文件中通过spring.profiles.active属性来设置,其值对应配置文件中的{profile}值。如:spring.profiles.active=test就会加载application-test.properties配置文件内容。

当然您也可以用命令行启动的时候带上参数:

java -jar 项目名称.jar --spring.profiles.active=dev

application.properties

spring.profiles.active=dev

application-dev.properties

blog.id=3
blog.title=SpringBoot profiles dev config
blog.author=fish
blog.desc=${blog.author} wrote an article about ${blog.title}

application-pro.properties

blog.id=4
blog.title=SpringBoot profiles pro config
blog.author=fish
blog.desc=${blog.author} wrote an article about ${blog.title}
@RestController
public class TestController {

  @Autowired
  private Blog blog;

  @RequestMapping("/test")
  public String test() {
    return blog.toString();
  }
}

@Data
@Component
public class Blog {
    @Value("${blog.id}")
    private Long id;
    @Value("${blog.title}")
    private String title;
    @Value("${blog.author}")
    private String author;
    @Value("${blog.desc}")
    private String desc;
}

在这里插入图片描述

三、YAML

YAML采用的配置格式不像properties的配置那样以单纯的键值对形式来表示,而是以类似大纲的缩进形式来表示。比如:下面的一段YAML配置信息

blog:
  id: 1
  title: SpringBoot
  author: fish

其他知识点和 properties 几乎一样,只是配置方式稍有不同,在这里YAML的使用我就不重复阐述了。

注意点:
(1)无法使用 @PropertySource 注解加载 YAML 文件。因此,如果您需要以这种方式加载值,请使用属性文件(properties)。
(2)SpringBoot 2.4版本之前,我们在 YAML 配置文件中,使用spring.profiles来定义不同环境的标识,而在2.4版本升级之后,我们需要将spring.profiles配置用spring.config.activate.on-profile替代。

SpringBoot 2.4 版本对配置文件加载机制做了很多变化,有兴趣的小伙伴可以去 Spring 官网查看。

四、源码

本文的相关例子可以查看下面仓库中的 chapter2 目录:

  • Gitee:https://gitee.com/hezhiyuan007/spring-boot-study
  • Github:https://github.com/java-fish-0907/spring-boot-study

点击全文阅读


本文链接:http://zhangshiyu.com/post/34262.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1