加载顺序

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

上图的所有配置信息都会以(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. Eclipse怎么切换工作空间

    1.进行点击Eclipse编辑代码的窗口界面中,进行点击菜单中file的选项. 2.弹出了下拉菜单中进行选择为“switch workspace”的选项. 3.弹出了下一级菜单中进行选择为other的 ...

  2. jmeter性能测试入门使用参数化

    我经常使用jmeter进行接口测试,这个工具还是很好用的.昨天收到一个需求,需要压测一下接口,jmeter进行接口测试,使用cvs文件进行多个数据参数化. 临时准备了一下发现忘记怎么做参数化了,自己百 ...

  3. Scala中做简易wordCount

    使用foldLeft函数,实现简易的wordCount import scala.collection.mutable object Demo_019 { def main(args: Array[S ...

  4. openstack nova 手动修改虚拟机状态

    source admin-openrc.sh nova list nova reset-state <id> --active

  5. 算法-搜索(5)m路搜索树

    动态m路搜索树即系统运行时可以动态调整保持较高搜索效率的最多m路的搜索树.以3路搜索树为例说明其关键码排序关系:   const int MaxValue=; template <class T ...

  6. docker run 创建容器

    docker run常用命令 docker run :创建一个新的容器并运行一个命令 - 语法:docker run [OPTIONS] IMAGE [COMMAND] [ARG...] 1.OPTI ...

  7. 数组的三种方式总结 多维数组的遍历 Arrays类的常用方法总结

    一.数组的三种声明方式总结 public class WhatEver { public static void main(String[] args) { //第一种 例: String[] tes ...

  8. 初识ABP vNext(6):vue+ABP实现国际化

    Tips:本篇已加入系列文章阅读目录,可点击查看更多相关文章. 目录 前言 开始 语言选项 语言切换 注意 最后 前言 上一篇介绍了ABP扩展实体,并且在前端部分新增了身份认证管理和租户管理的菜单,在 ...

  9. Java I/O体系从原理到应用(非原创)

    基础概念 在介绍I/O原理之前,先重温几个基础概念: 1 操作系统与内核 操作系统:管理计算机硬件与软件资源的系统软件内核:操作系统的核心软件,负责管理系统的进程.内存.设备驱动程序.文件和网络系统等 ...

  10. 【WEB自动化测试之控件定位】基于HTML5控件的唯一控件属性定位

      一.WEB控件定位是什么 要想弄懂这个问题,我们还是基于实践来学习.我们先来看一条入门级别自动化测试用例的构成. DemoCase:正确用户名和密码登录博客园,登录成功 URL: https:// ...