• SpringBoot系列之外部配置用法简介


    SpringBoot系列之外部配置用法简介

    引用Springboot官方文档的说法,官方文档总共列举了如下用法:

    1、Devtools global settings properties on your home directory (~/.spring-boot-devtools.properties when devtools is active).
    2、@TestPropertySource annotations on your tests.
    3、properties attribute on your tests. Available on @SpringBootTest and the test annotations for testing a particular slice of your application.
    4、Command line arguments.
    5、Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).
    6、ServletConfig init parameters.
    7、ServletContext init parameters.
    8、JNDI attributes from java:comp/env.
    9、Java System properties (System.getProperties()).
    10、OS environment variables.
    11、A RandomValuePropertySource that has properties only in random.*.
    12、Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants).
    13、Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).
    14、Application properties outside of your packaged jar (application.properties and YAML variants).
    15、Application properties packaged inside your jar (application.properties and YAML variants).
    16、@PropertySource annotations on your @Configuration classes.
    17、Default properties (specified by setting SpringApplication.setDefaultProperties).

    Springboot官方文档应经将这种用法做了比较详细的描述:Springboot外部配置用法官方文档,所以本博客只简单介绍一下比较常用的

    尚硅谷有位老师对这些用法进行梳理,整理出比较常用的几种用法:

    这种外部配置的优先级按照从高到低的顺序排序如下图所示,优先级高的属性配置会覆盖优先级低的配置,而且存在互补配置的特性,对于这些特性可以参考我上篇博客:SpringBoot系列之配置文件加载位置

    在这里插入图片描述

    注意:

    • 命令行配置的方式
      比如java -jar这种方式,多个属性之间用空格分开,properties里的属性基本都能用,用法是属性项=属性值这种方法,举个例子:
    java -jar springboot-properties-config-0.0.1-SNAPSHOT.jar --server.port=8082 --server.context-path=/example
    

    在这里插入图片描述
    如果都通过命令行这种方法,一个属性一个属性的加,显然不太可行,所以可以在jar包所在的文件夹下面加个application.properties/application.yml,然后再通过命令启动,就可以自动加载配置文件里的配置
    在这里插入图片描述

    • application.properties或者application.yml
      对于application.properties或者application.yml配置文件可以对其进行归类,可以分为jar包外部的和内部的,也可以分为带${profile}的和不带${profile}的,profile的可以参考我上篇博客:Springboot系列之profile多环境配置用法简介

    一般来说jar外部的配置优先级一般是高于jar包内部的的,profile的配置文件优先级也是高于不到profile的application.proerties/application.yml配置文件的,所以针对配置文件加载顺序优先级进行排序

    jar外部配置文件被加载的优先级高于jar内部的,${profile}的配置文件被加载优先级高于没带${profile}的配置文件

    • jar包外部的application-${profile}.properties或者application-${profile}.yml
    • jar包内部的application-${profile}.properties或者application-${profile}.yml
    • jar包外部的application.properties或者application.yml
    • jar包内部的application.properties或者application.yml
  • 相关阅读:
    修改系统策略 作为服务登录
    PlayStation Suite(PSS)快速预览
    Python异常处理
    误操作把/bin目录移动到其他目录下的恢复方法
    SQL Prompt 自动过期反复激活防失效注册后隔一段时间莫名无法使用的处理
    [转帖]SQL SERVER 2005 同步复制技术
    动网论坛修改笔记
    网线的做法
    风讯修改记录
    远程桌面中调出任务管理器
  • 原文地址:https://www.cnblogs.com/mzq123/p/11830415.html
Copyright © 2020-2023  润新知