Spring Boot 所有相关的配置信息
加载顺序

如上图所示,图片是从官网上截取的,这些配置信息都会加载,只不过顺序在前的会覆盖掉后面的
上图的所有配置信息都会以(key,value)的形式加载到Spring中的Environment中,也可以供@Value和@ConfigurationProperties注解使用
本文只介绍在@PropertySource注解导入的、properties文件中的、yml文件中的、操作系统的变量、Java System properties、命令行中的配置信息
配置信息媒介
- 命令行中的配置信息:使用
--,在idea中配置Program arguments,下面是使用命令行来添加
$ java -jar myproject.jar --server.port=8090
- Java System properties:使用
-D,在idea中配置VM options,下面是使用命令行来添加
$ java -jar myproject.jar -Dserver.port=8090
- 操作系统的变量:
vim ~/.bash_profile
在文件中添加 export SERVER_PORT=8090 并保存
source ~/.bash_profile
- properties文件
server.port=8090
spring.profiles.active=dev
- yml文件
server:
port: 8090
- @PropertySource (在@Configuration下,必须是properties文件,yml文件不支持)
@Configuration
@PropertySource("classpath:config.properties")
public class Config {
}
加载properties文件和yml文件
默认
- properties文件和yml文件都会加载,对于相同属性,我理解properties文件应该会覆盖掉yml文件
- 默认会加载
application.properties和application.yml - 如果没有指定
spring.profiles.active默认会加载application-default.properties和application-default.yml,如果指定了xxx,则加载application-xxx.properties和application-xxx.yml - 会从以下六个默认文件夹找这些配置文件:
file:./config/、file:./config/*/、file:./、classpath:/config/、classpath:/config/*/、classpath:/。我理解前三个是jar包外,后三个是jar包内,jar包外的file文件夹我理解是指执行命令所在的文件夹。 - 优先级:
jar包外application-xxx文件->jar包内application-xxx文件->jar包外application文件->jar包内application文件
指定
spring.config.name修改配置文件名称,也就是不加载application文件了,如果指定了xxx文件,则加载xxx.properties和xxx.yml文件spring.config.location指定特定文件和特定文件夹,不再加载默认文件夹spring.config.additional-location指定特定的文件夹和特定的文件,会加载默认文件夹
spring.config.name and spring.config.location are used very early to determine which files have to be loaded. They must be defined as an environment property (typically an OS environment variable, a system property, or a command-line argument).
意思是说只能通过操作系统变量、java system property和命令行来配置才起作用
Relaxed Binding
配置文件中的变量名不用非得与class中的变量名一致,对于点与点之间的变量名,可以使用_、-、驼峰 、大小写这些形式,
userName = user_name = user-name = USERNAME = USER_NAME = USER-NAME
下图是各个形式的实例

The prefix value for the annotation must be in kebab case (lowercase and separated by -, such as acme.my-project.person).
下图是每个文件类型所支持的形式(Upper case format 只适用于操作系统的变量中)

使用配置信息
environment
直接注入
@Autowired
private Environment environment;
environment.getProperty("server.port");
@value
If you do want to use @Value, we recommend that you refer to property names using their canonical form (kebab-case using only lowercase letters). This will allow Spring Boot to use the same logic as it does when relaxed binding @ConfigurationProperties. For example, @Value("{demo.item-price}") will pick up demo.item-price and demo.itemPrice forms from the application.properties file, as well as DEMO_ITEMPRICE from the system environment. If you used @Value("{demo.itemPrice}") instead, demo.item-price and DEMO_ITEMPRICE would not be considered.
建议使用kebab-case形式,这样会配到更多的值
@Component
public class ValueBean {
@Value("${server.port}")
private String serverPort;
}
@ConfigurationProperties
@ConfigurationProperties("server")
public class ConfigurationPropertiesBean {
private Integer port;
}
对于想要注入这个类的话,有以下几种方式
使用@EnableConfigurationProperties
@Configuration
@EnableConfigurationProperties({ConfigurationPropertiesBean.class})
public class Config {
}
使用@ConfigurationPropertiesScan
@Configuration
@ConfigurationPropertiesScan({"com.example.demo.spring.boot.externalized.configuration"})
public class Config {
}
使用@component
@ConfigurationProperties("server")
@Component
public class ConfigurationPropertiesBean {
private Integer port;
}
使用@Bean
@Bean
@ConfigurationProperties("server")
public ConfigurationPropertiesBean configurationPropertiesBean() {
return new ConfigurationPropertiesBean();
}
参考
Spring Boot 所有相关的配置信息的更多相关文章
- Spring Boot实践——用外部配置填充Bean属性的几种方法
引用:https://blog.csdn.net/qq_17586821/article/details/79802320 spring boot允许我们把配置信息外部化.由此,我们就可以在不同的环境 ...
- 转-spring boot web相关配置
spring boot web相关配置 80436 spring boot集成了servlet容器,当我们在pom文件中增加spring-boot-starter-web的maven依赖时,不做任何w ...
- Spring Boot 2.X(十六):应用监控之 Spring Boot Actuator 使用及配置
Actuator 简介 Actuator 是 Spring Boot 提供的对应用系统的自省和监控功能.通过 Actuator,可以使用数据化的指标去度量应用的运行情况,比如查看服务器的磁盘.内存.C ...
- Spring boot 基于注解方式配置datasource
Spring boot 基于注解方式配置datasource 编辑 Xml配置 我们先来回顾下,使用xml配置数据源. 步骤: 先加载数据库相关配置文件; 配置数据源; 配置sqlSessionF ...
- Spring Boot 探索系列 - 自动化配置篇
26. Logging Prev Part IV. Spring Boot features Next 26. Logging Spring Boot uses Commons Logging f ...
- Spring Boot 2.0 教程 | 配置 Undertow 容器
欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 资深架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 文章首发于个人网站 https://ww ...
- spring boot @ConditionalOnxxx相关注解总结
Spring boot @ConditionalOnxxx相关注解总结 下面来介绍如何使用@Condition public class TestCondition implements Condit ...
- Spring Boot 2.X(四):Spring Boot 自定义 Web MVC 配置
0.准备 Spring Boot 不仅提供了相当简单使用的自动配置功能,而且开放了非常自由灵活的配置类.Spring MVC 为我们提供了 WebMvcConfigurationSupport 类和一 ...
- Spring Boot2 系列教程(十八)Spring Boot 中自定义 SpringMVC 配置
用过 Spring Boot 的小伙伴都知道,我们只需要在项目中引入 spring-boot-starter-web 依赖,SpringMVC 的一整套东西就会自动给我们配置好,但是,真实的项目环境比 ...
随机推荐
- java.lang.NoSuchFieldError: No static field XXX of type I in class Lcom/XX/R$id; or its superclasses
报错: 当启动一个页面的时候报错: java.lang.NoSuchFieldError: No static field XXX of type I in class Lcom/XXX/R$id; ...
- 送大家几个cnblogs号,供快捷评论,请谨慎使用
欢迎评论& 3461896724@qq.com互动 可以在我的首页看更多 #1先送大家几个号:(密码都是 MLdlight2020)请区分大小写(可以直接复制) 写过一篇 免费验证码接收网站& ...
- JavaScript学习系列博客_9_JavaScript中的if语句、switch语句
条件判断语句 - 条件判断语句也称为if语句 - 语法一: if(条件表达式){ 语句... } - 执行流程: if语句执行时,会先对条件表达式进行求值判断, 如果值为true,则执行if后的语句 ...
- 要有一颗理财的心 - 读<富爸爸.穷爸爸>
记得工作没多久后的一次加薪的例行谈话.部门经理和我说,不能靠工资过日子,要多想想怎么投资,我的主要财富就是靠投资赚来的.当时第一反应,老板,你不给我加薪找这借口也太牵强了吧.我的收入只有工资,我的工资 ...
- Kubernetes 多集群在开源项目 KubeSphere 的应用
Kubernetes 多集群使用场景 随着容器的普及和 Kubernetes 的日渐成熟,企业内部运行多个 Kubernetes 集群已变得颇为常见.概括起来,多个集群的使用场景主要有以下几种. 多集 ...
- 算法-heapq模块优先队列
heapq模块, 优先队列,小顶堆,最少值放在顶部,值越小,优先级越高 heapq.heappop(heap) 从堆中弹出最小的元素,并重新调整 heapq.heappush(heap, item)新 ...
- 算法-搜索(3)AVL树
AVL树高度平衡的二叉搜索树,任一点的平衡印章只能是+1.-1.0,从而尽量降低树的高度. 如果它有n个结点,高度可保持在O(log2n),平均搜索长度也可保持在O(log2n). (1)AVL树的插 ...
- Logistic回归分析之多分类Logistic回归
Logistic回归分析(logit回归)一般可分为3类,分别是二元Logistic回归分析.多分类Logistic回归分析和有序Logistic回归分析.logistic回归分析类型如下所示. Lo ...
- Jenkins匿名用户设置
最近自己安装配置jenkins,但是跑任务时,发现是匿名账户登录,可以在系统设置中点击如下: 2.勾选“启用安全”,“访问控制”>“安全域”选择“Jenkins专有用户数据库”,并勾选“允许用户 ...
- Java BigDecimal使用指南
提起BigDecimal,相信大家都使用过,之所以总结这篇呢,是因为最近发现项目中使用的不是太规范,在某些场景下甚至出现代码抛出异常的情况, 所以就总结了这篇,希望大家在使用时,可以少踩一些坑. 1. ...