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
SpringBoot系列之外部配置用法简介的更多相关文章
- SpringBoot系列之@Conditional注解用法简介
SpringBoot系列之@Conditional注解用法简介 引用Spring官方文档的说法介绍一下@Conditional注解:Spring5.0.15版本@Conditional注解官方文档 @ ...
- SpringBoot系列之YAML配置用法
1.全局配置 SpringBoot的全局配置文件有两种: application.properties application.yml 配置文件的作用:修改SpringBoot自动配置的默认值,主要是 ...
- SpringBoot系列之profles配置多环境(篇二)
SpringBoot系列之profles配置多环境(篇二) 继续上篇博客SpringBoot系列之profles配置多环境(篇一)之后,继续写一篇博客进行补充 写Spring项目时,在测试环境是一套数 ...
- SpringBoot系列之集成Thymeleaf用法手册
目录 1.模板引擎 2.Thymeleaf简介 2.1).Thymeleaf定义 2.2).适用模板 3.重要知识点 3.1).th:text和th:utext 3.2).标准表达式 3.3).Thy ...
- SpringBoot 系列教程自动配置选择生效
191214-SpringBoot 系列教程自动配置选择生效 写了这么久的 Spring 系列博文,发现了一个问题,之前所有的文章都是围绕的让一个东西生效:那么有没有反其道而行之的呢? 我们知道可以通 ...
- 3springboot:springboot配置文件(外部配置加载顺序、自动配置原理,@Conditional)
1.外部配置加载顺序 SpringBoot也可以从以下位置加载配置: 优先级从高到低 高优先级的配置覆盖低优先级的配置,所有的配置会形成互补配置 1.命令行参数 所有的配置都可以在命令行上进行指定 ...
- SpringBoot系列之profles配置多环境(篇一)
SpringBoot profles配置多环境 23/100 发布文章 u014427391 软件环境简介 这里介绍一下SpringBoot提供的profiles属性加上maven配置一下多环境,在实 ...
- springboot系列四、配置模板引擎、配置热部署
一.配置模板引擎 在之前所见到的信息显示发现都是以 Rest 风格进行显示,但是很明显在实际的开发之中,所有数据的显示最终都应该交由页面完成,但是这个页面并不是*.jsp 页面,而是普通的*.html ...
- SpringBoot系列之学习教程汇总
对应SpringBoot系列博客专栏,例子代码,本博客不定时更新 一.配置篇 SpringBoot系列之@PropertySource读取yaml文件 >> source down ...
随机推荐
- 12.2新特性 使用DBCA duplicate创建物理备用数据库 (Doc ID 2283697.1)
Creating a Physical Standby database using DBCA duplicate (Doc ID 2283697.1) APPLIES TO: Oracle Data ...
- 剑指Offer-45.扑克牌顺子(C++/Java)
题目: LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定 ...
- [译]Vulkan教程(24)索引buffer
[译]Vulkan教程(24)索引buffer Index buffer 索引buffer Introduction 入门 The 3D meshes you'll be rendering in a ...
- python强大的绘图模块matplotlib示例讲解
Matplotlib 是 Python 的绘图库.作为程序员,经常需要进行绘图,在我自己的工作中,如果需要绘图,一般都是将数据导入到excel中,然后通过excel生成图表,这样操作起来还是比较繁琐的 ...
- Linux中,Tomcat 怎么承载高并发(深入Tcp参数 backlog)
一.前言 这两天看tomcat,查阅 tomcat 怎么承载高并发时,看到了backlog参数.我们知道,服务器端一般使用mq来减轻高并发下的洪峰冲击,将暂时不能处理的请求放入队列,后续再慢慢处理.其 ...
- mysql 数据误删恢复
当binlog_format设置为ROW时,想查看binlog中的sql语句时,需要解密,添加这个 --base64-output=decode-rows -v
- Jquery中的done() fail() then() $when()到底是什么
ajax的传统写法: $.ajax({ url: "test.html", success: function(){ alert("哈哈,成功了!"); }, ...
- SpringCloud的入门学习之Eureka(高可用注册中心HA)构建Provider服务、Consumer服务
1.在高可用的Eureka注册中心中构建provider服务. 使用springboot的多环境配置,来搭建Eureka的高可用集群式部署.由于使用的是maven构建的springboot项目,所以首 ...
- C# 管道通信 (client —— server)Framework2.0版本也可用
//管道服务类public class PipServer { [DllImport("kernel32.dll", SetLastError = true)] public st ...
- python 使用tesseract进行图片识别
from PIL import Image import pytesseract text = pytesseract.image_to_string(Image.open(r'E:\guo\2432 ...