加载顺序

如上图所示,图片是从官网上截取的,这些配置信息都会加载,只不过顺序在前的会覆盖掉后面的

上图的所有配置信息都会以(key,value)的形式加载到Spring中的Environment中,也可以供@Value@ConfigurationProperties注解使用

本文只介绍在@PropertySource注解导入的、properties文件中的、yml文件中的、操作系统的变量、Java System properties命令行中的配置信息

配置信息媒介

  1. 命令行中的配置信息:使用--,在idea中配置Program arguments,下面是使用命令行来添加
$ java -jar myproject.jar --server.port=8090
  1. Java System properties:使用-D,在idea中配置VM options,下面是使用命令行来添加
$ java -jar myproject.jar -Dserver.port=8090
  1. 操作系统的变量:
vim ~/.bash_profile
在文件中添加 export SERVER_PORT=8090 并保存
source ~/.bash_profile
  1. properties文件
server.port=8090
spring.profiles.active=dev
  1. yml文件
server:
port: 8090
  1. @PropertySource (在@Configuration下,必须是properties文件,yml文件不支持)
@Configuration
@PropertySource("classpath:config.properties")
public class Config {
}

加载properties文件和yml文件

默认

  • properties文件和yml文件都会加载,对于相同属性,我理解properties文件应该会覆盖掉yml文件
  • 默认会加载application.propertiesapplication.yml
  • 如果没有指定spring.profiles.active 默认会加载application-default.propertiesapplication-default.yml,如果指定了xxx,则加载application-xxx.propertiesapplication-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();
}

参考

boot-features-external-config

Spring Boot 所有相关的配置信息的更多相关文章

  1. Spring Boot实践——用外部配置填充Bean属性的几种方法

    引用:https://blog.csdn.net/qq_17586821/article/details/79802320 spring boot允许我们把配置信息外部化.由此,我们就可以在不同的环境 ...

  2. 转-spring boot web相关配置

    spring boot web相关配置 80436 spring boot集成了servlet容器,当我们在pom文件中增加spring-boot-starter-web的maven依赖时,不做任何w ...

  3. Spring Boot 2.X(十六):应用监控之 Spring Boot Actuator 使用及配置

    Actuator 简介 Actuator 是 Spring Boot 提供的对应用系统的自省和监控功能.通过 Actuator,可以使用数据化的指标去度量应用的运行情况,比如查看服务器的磁盘.内存.C ...

  4. Spring boot 基于注解方式配置datasource

    Spring boot 基于注解方式配置datasource 编辑 ​ Xml配置 我们先来回顾下,使用xml配置数据源. 步骤: 先加载数据库相关配置文件; 配置数据源; 配置sqlSessionF ...

  5. Spring Boot 探索系列 - 自动化配置篇

    26. Logging Prev  Part IV. Spring Boot features  Next 26. Logging Spring Boot uses Commons Logging f ...

  6. Spring Boot 2.0 教程 | 配置 Undertow 容器

    欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 资深架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 文章首发于个人网站 https://ww ...

  7. spring boot @ConditionalOnxxx相关注解总结

    Spring boot @ConditionalOnxxx相关注解总结 下面来介绍如何使用@Condition public class TestCondition implements Condit ...

  8. Spring Boot 2.X(四):Spring Boot 自定义 Web MVC 配置

    0.准备 Spring Boot 不仅提供了相当简单使用的自动配置功能,而且开放了非常自由灵活的配置类.Spring MVC 为我们提供了 WebMvcConfigurationSupport 类和一 ...

  9. Spring Boot2 系列教程(十八)Spring Boot 中自定义 SpringMVC 配置

    用过 Spring Boot 的小伙伴都知道,我们只需要在项目中引入 spring-boot-starter-web 依赖,SpringMVC 的一整套东西就会自动给我们配置好,但是,真实的项目环境比 ...

随机推荐

  1. Unix I/O

    Unix I/O 打开文件 一个应用程序通过要求内核打开相应的文件,来宣告它想要访问一个I/O设备.内核返回一个小的非负整数,叫做描述符,它在后续对此文件的所有操作中标识这个文件.内核记录有关这个打开 ...

  2. puppeteer去掉同源策略及请求拦截

    puppeteer是一个功能强大的工具,在自动化测试和爬虫方面应用广泛,这里谈一下如何在puppeteer中关掉同源策略和进行请求拦截. 同源策略 同源策略为web 安全提供了有力的保障,但是有时候我 ...

  3. 2 Spark角色介绍及运行模式

    第2章 Spark角色介绍及运行模式 2.1 集群角色 从物理部署层面上来看,Spark主要分为两种类型的节点,Master节点和Worker节点:Master节点主要运行集群管理器的中心化部分,所承 ...

  4. HDFS的数据流读写数据 (面试开发重点)

    1 HDFS写数据流程 1.1 剖析文件写入 HDFS写数据流程,如图所示 1)客户端通过Distributed FileSystem模块向NameNode请求上传文件,NameNode检查目标文件是 ...

  5. 第2篇 Scrum 冲刺博客

    1.站立会议 照骗 进度 成员 昨日完成任务 今日计划任务 遇到的困难 钟智锋 无 确定客户端和服务器通信的形式 各成员的代码难以统一 庄诗楷 无 编写客户端UI 加入图片总是失败 易德康 无 马,车 ...

  6. Linux服务器上创建新用户

    一.在/home目录下新建userName目录 sudo useradd -m -s /bin/bash userName 二.设置密码 sudo passwd userName

  7. 自动部署Asp.Net Core到Docker

    原文链接:个人博客:自动部署Asp.Net Core至Docker 本文简介 最近在开发一个管理系统,代码框架是用的前后台分离的方式 后台使用的是Asp.Net Core平台,开发所有业务,向前台提供 ...

  8. 更好地使用google

    精确搜索:双引号 精确搜索就是在你要搜索的词上,加上双引号,这时google就会完全的匹配你所要搜索的字符串 "今日黄瓜" 站内搜索:site 例如我想在stackoverflow ...

  9. PHP学习中的一些总结(持续更新)

    文件上传部分 在前台的<form>表单中 hidden隐藏域的MAX_FILE_SIZE可以起到实质性的控制作用,即在文件上传之前就可以判断文件的大小,格式为: <form acti ...

  10. 《spring源码解读》 - ioc之验证模型获取

    我们上一篇文章最后调用到 `org.springframework.beans.factory.xml. XmlBeanDefinitionReader#doLoadDocument(...) ` 方 ...