【Spring Boot配置文件简介】

本文介绍了SpringBoot配置文件的自动加载机制,包括默认配置文件、多环境支持,以及属性优先级规则。还讲述了如何通过命令行参数、环境变量和配置文件注入属性,以及不同文件类型的解析过程。详参官方文档:spring-boot-docs和配置文件加载流程。
摘要由CSDN通过智能技术生成

Spring Boot配置文件简介

简单介绍一下spring boot的配置文件吧,主要介绍一下配置文件的优先级以及配置文件的使用(ps:重点看3、4点吧),后面我会写一篇关于配置文件的源码分析文章,今天就先给大家热一下身,自己今天也摸一下鱼,嘿嘿!!!

  1. 自动加载: Spring Boot会自动加载默认的配置文件。对于properties文件,Spring Boot会默认加载application.properties文件;对于YAML文件,Spring Boot会默认加载application.ymlapplication.yaml文件。这些文件可以放置在classpath下的/config目录或者与jar包同级目录,统计目录的config,以及config下面的直接子目录。

  2. 多环境配置: Spring Boot支持基于不同环境的配置文件。通过使用spring.profiles.active属性,可以指定当前活动的配置文件环境。例如,可以创建application-dev.propertiesapplication-dev.yml文件,用于开发环境的配置。Spring Boot会根据spring.profiles.active的值自动加载相应的配置文件。

  3. 属性优先级 在Spring Boot中,属性的优先级由高到低依次为:命令行参数 > 系统属性 > 环境变量 > 配置文件中的属性。这意味着可以通过命令行参数、环境变量或系统属性来覆盖配置文件中的属性值。

    官方文档描述, 后面的属性源可以覆盖前面定义的值。按以下顺序考虑来源:

    • Default properties (specified by setting SpringApplication.setDefaultProperties).
    • @PropertySource annotations on your @Configuration classes. Please note that such property sources are not added to the Environment until the application context is being refreshed. This is too late to configure certain properties such as logging.* and spring.main.* which are read before refresh begins.
    • Config data (such as application.properties files).(配置文件)
    • A RandomValuePropertySource that has properties only in random.*.
    • OS environment variables.(环境变量)
    • Java System properties (System.getProperties()).(java系统属性)
    • JNDI attributes from java:comp/env.
    • ServletContext init parameters.
    • ServletConfig init parameters.
    • Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).
    • Command line arguments.(命令行参数)
    • properties attribute on your tests. Available on @SpringBootTest and the test annotations for testing a particular slice of your application.
    • @DynamicPropertySource annotations in your tests
    • @TestPropertySource annotations on your tests.
    • Devtools global settings properties in the $HOME/.config/spring-boot directory when devtools is active.
  4. 配置文件加载顺序 Spring Boot会按照一定的顺序加载配置文件。首先,会加载默认的配置文件,然后根据spring.profiles.active加载对应的环境配置文件。最后,会加载其他的外部化配置文件。首先加载jar包内的配置文件(yml、properties同时存在会使用properties),再加载外部配置文件,后加载的配置覆盖先加载的文件。
    配置文件加载顺序如下:后面的配置覆盖前面的配置(ps: 外部配置覆盖内部配置,同级目录config下面的覆盖外面的)。

    • classPath下面配置
    • classpathc下面config文件夹下面的配置
    • jar包所在路劲的配置
    • jar包所在路劲config下的配置

    eg:应用会使用config下面的properties
    在这里插入图片描述

  5. 属性注入: Spring Boot支持将配置文件中的属性值注入到应用程序中的Bean中。通过使用@Value注解或@ConfigurationProperties注解,可以将属性值注入到相应的字段或方法参数中。@Value注解适用于单个属性的注入,而@ConfigurationProperties注解适用于将一组属性值注入到一个自定义的配置类中。

  6. 配置文件解析: Spring Boot使用不同的解析器来解析不同类型的配置文件。对于properties文件,Spring Boot使用PropertiesLoaderUtils来解析文件内容;对于YAML文件,Spring Boot使用SnakeYAML库来解析文件内容。

配置文件相关的详细知识可以参考官网:https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config.files
配置文件加载流程参考:https://editor.csdn.net/md/?articleId=135341539

  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不务专业的程序员--阿飞

兄弟们能否给口饭吃

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值