spring编程:springboot属性配置和属性优先级

志波同学      2022-06-07     643

关键词:

Spring Boot 允许我们外部化程序的配置,以便我们可以在不同的环境中使用相同的应用代码。我们可以使用 properties、YAML、环境变量、命令行参数进行外部化配置。配置的属性值可以通过 @Value 注解直接注入到 beans 中,通过 Spring 环境抽象访问;也可以通过 @ConfigurationProperties 绑定到结构化对象中。

Spring Boot 使用一个非常特殊的 PropertySource 顺序,该顺序用来合理的覆盖属性值。属性优先级由高到低排序如下:

1 .spring-boot-devtools.properties

Devtools global settings properties on your home directory (~/.spring-boot-devtools.properties when devtools is active).

home 目录下的 .spring-boot-devtools.properties 文件优先级最高,但是需要激活 devtools。

2 @TestPropertySource

@TestPropertySource annotations on your tests.

测试时使用。

3 @SpringBootTest

properties attribute on your tests. Available on @SpringBootTest and the test annotations for testing a particular slice of your application.

4 Command line

Command line arguments.

5 SPRING_APPLICATION_JSON

Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).
在环境变量、系统属性、命令行参数、JNDI 中配置的 SPRING_APPLICATION_JSON 属性。在 Spring Boot 的文档中有说明,命令行参数、JNDI 中配置的 SPRING_APPLICATION_JSON 也会生效。

6 ServletConfig

ServletConfig init parameters.

7 ServletContext

ServletContext init parameters.

8 JNDI

JNDI attributes from java:comp/env.

9 Java System properties

Java System properties (System.getProperties()).
Java系统属性。

10 OS environment variables

OS environment variables.
操作系统环境变量。

11 RandomValuePropertySource

A RandomValuePropertySource that has properties only in random.*.

12 outside Profile-specific application properties

Profile-specific application properties outside of your packaged jar (application-profile.properties and YAML variants).

13 inside Profile-specific application properties

Profile-specific application properties packaged inside your jar (application-profile.properties and YAML variants).

14 outside Application properties

Application properties outside of your packaged jar (application.properties and YAML variants).

15 inside Application properties

Application properties packaged inside your jar (application.properties and YAML variants).

16 @PropertySource

@PropertySource annotations on your @Configuration classes.

17 Default properties

Default properties (specified by setting SpringApplication.setDefaultProperties).

以上内容参考 Spring Boot 官方文档进行整理,文章内容仅代表个人观点,如有不正之处,欢迎批评指正,谢谢大家。

参考文档

Externalized Configuration

Spring Boot 以编程方式设置配置文件

】SpringBoot以编程方式设置配置文件【英文标题】:SpringBootProgrammaticallysettingprofiles【发布时间】:2015-09-2420:45:05【问题描述】:如何在SpringBoot应用程序中设置活动配置文件。此应用程序将部署在独立的Tomcat中。我有2个属性文件a... 查看详情

以编程方式配置 Spring Boot 应用程序

】以编程方式配置SpringBoot应用程序【英文标题】:ProgrammaticallyconfigureSpringBootapp【发布时间】:2017-02-2411:20:59【问题描述】:获得SpringBoot优点但不尝试自动配置所有内容的最简单方法是什么?例如,要仅使用我们已经配置的属... 查看详情

springboot01,spring完全注解(零配置)编程方式

前言:   springboot就是整合了很多优秀的框架,不用我们自己手动的去写一堆xml配置然后进行配置。有了它,你可以更加敏捷地开发Spring应用程序,专注于应用程序的功能,不用在Spring的配置上多花功夫,甚至完全不用配... 查看详情

springboot配置属性之spring.profiles.include

参考技术Aspring.profiles.active属性和其他属性一样都遵循相同的排列规则,最高的PropertySource获胜。也就是说,你可以在application.properties中指定生效的配置,然后使用命令行开关替换它们。有时,将特定的配置属性添加到生效的配... 查看详情

springboot属性配置和使用(代码片段)

SpringBoot属性配置和使用SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。简单说就... 查看详情

springboot属性配置和使用

...就是可以通过配置文件来注入属性或者修改默认的配置。SpringBoot系列SpringBoot入门SpringBoot属性配置和使用SpringBoot集成MyBatisSpringBoot静态资源处理SpringBoot-配置排序依赖技巧SpringBoot-DevTools介绍SpringBoot支持 查看详情

Spring Boot:自定义属性配置和测试

】SpringBoot:自定义属性配置和测试【英文标题】:SpringBoot:custompropertiesconfigurationandtests【发布时间】:2018-11-1400:49:27【问题描述】:我正在使用带有默认application.yml属性文件的SpringBoot2.0。我想将它拆分为单独的属性文件,因为... 查看详情

Spring Boot 自动配置加载 Spring Kafka 属性失败

】SpringBoot自动配置加载SpringKafka属性失败【英文标题】:SpringBootAutoConfigurationFailedLoadingSpringKafkaProperties【发布时间】:2018-03-1016:57:35【问题描述】:Springboot无法加载属性。这是我通过yaml文件使用的属性。spring:kafka:bootstrap-servers... 查看详情

Spring Boot 程序化日志配置

】SpringBoot程序化日志配置【英文标题】:SpringBootprogrammaticloggingconfiguration【发布时间】:2013-12-2817:30:47【问题描述】:如何在SpringBoot应用程序中以编程方式配置日志记录?使用xml或属性文件对我的需要不够灵活。更新:我想实... 查看详情

从云配置服务器配置 spring.* 启动属性

...找不到直接的答案。如何从CloudConfigServer实例配置基本的SpringBoot属性?显然,与配置服务器相关的Springboot属性(即cloud.conf 查看详情

springboot的配置文件有哪几种格式?

SpringBoot中的配置文件主要有三种格式,properties、yaml、和xml方式。-其中properties格式配置文件后缀是.properties,配置项为:server.port=9090-yaml格式配置文件后缀是.yml,配置项是:server.port:9090在SpringBoot中,使用最广泛的配置文件是ya... 查看详情

如何使用 Spring Boot 配置属性对属性进行分组

】如何使用SpringBoot配置属性对属性进行分组【英文标题】:HowtogrouppropertieswithSpringBootConfigurationProperties【发布时间】:2016-03-0717:05:52【问题描述】:根据SpringBoot文档,可以对属性进行分组,并且一个属性可能出现在多个组中。... 查看详情

spring和springboot的属性(代码片段)

...tySource或通过XML和<property-placeholder>,以及属性如何在SpringBoot中工作。2.通过Java注解注册属性文件Spring3.1还引入了新的@PropertySource注解,作为向环境添加属性源的便捷机制。此注解将与基于Java的 查看详情

Spring Boot:使用 yaml 中配置的 Maven 属性

】SpringBoot:使用yaml中配置的Maven属性【英文标题】:SpringBoot:useofMavenpropertiesconfiguredinyaml【发布时间】:2017-11-0712:31:05【问题描述】:我正在尝试从Maven中提取一些属性并在我的应用程序启动时打印它们。我正在使用SpringBoot,因... 查看详情

springboot31重识spring01-环境搭建actuator监控属性配置多环境配置

 1前言1.1学习阶段说明    从2016年9月开始接触IT,学习经历主要分为以下三个阶段  1.1.1入门阶段    从最基础的前端技术HTML、JavaScript、CSS开始入门,再到后端技术Java基础、MySQL数据库基础知识、JDBC、Servclet、JSP... 查看详情

如何以编程方式覆盖 Spring Boot application.properties?

】如何以编程方式覆盖SpringBootapplication.properties?【英文标题】:HowcanIoverrideSpringBootapplication.propertiesprogrammatically?【发布时间】:2015-05-1807:40:11【问题描述】:我有从外部配置web服务获取的jdbc属性文件在SpringBoot中,为了设置mysq... 查看详情

依赖于配置文件特定属性的 Spring Boot 属性

】依赖于配置文件特定属性的SpringBoot属性【英文标题】:SpringBootpropertiesthatdependonprofile-specificproperties【发布时间】:2020-12-2118:20:30【问题描述】:我有一个SpringBoot应用程序,它应该连接到dev和prod中的不同服务器,并且在这些... 查看详情

在 Spring Boot 中使用默认模式以编程方式配置 OracleDataSource

】在SpringBoot中使用默认模式以编程方式配置OracleDataSource【英文标题】:ConfigureOracleDataSourceprogrammaticallyinSpringBootwithadefaultschema【发布时间】:2018-07-1216:29:36【问题描述】:如何在SpringBoot中使用默认架构以编程方式配置OracleDataSou... 查看详情