资讯专栏INFORMATION COLUMN

spring boot学习(6)— 配置信息及其读取优先级

stormzhang / 3048人阅读

摘要:优先级如下使用文件使用文件,会根据以下目录去寻找,添加到中,优先级依次递增。目录下目录工程根目录工程跟目录下的目录加载顺序从优先级高的先加载。属性值怎么取优先级高的会覆盖优先级低的。但是在同等目录下,优先级高于文件的配置信息。

1. properties 信息从哪里取

在不同的环境,我们需要使用不同的配置,Spring boot 已经提供了相关功能,可以是 properties 文件, yaml 文件 或是命令行参数。优先级如下

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

@TestPropertySource annotations on your tests.

@SpringBootTest#properties annotation attribute on your tests.

Command line arguments.

java -jar app.jar --name="Spring"

Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).

environment vaiable:SPRING_APPLICATION_JSON="{"acme":{"name":"test"}}" java -jar myapp.jar

command line:

java -Dspring.application.json="{"name":"test"}" -jar myapp.jar

java -jar myapp.jar --spring.application.json="{"name":"test"}"

ServletConfig init parameters.

ServletContext init parameters.

JNDI attributes from java:comp/env.

Java System properties (System.getProperties()).

OS environment variables.

A RandomValuePropertySource that has properties only in random.*.

my.secret=${random.value}
my.number=${random.int}
my.bignumber=${random.long}
my.uuid=${random.uuid}
my.number.less.than.ten=${random.int(10)}
my.number.in.range=${random.int[1024,65536]}

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

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

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

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

@PropertySource annotations on your @Configuration classes.

Default properties (specified by setting SpringApplication.setDefaultProperties).

2. 使用 application.properties 文件

使用 properties 文件,spring boot 会根据以下目录去寻找,添加到 Spring Environment 中,优先级依次递增。

classpath:/: resources 目录

classpath:/config/: resourcesconfig 目录

file:./:工程根目录

file:./config/: 工程跟目录下的 config 目录

2.1 加载顺序:

从优先级高的先加载。

file:./config/

file:./

classpath:/config/

classpath:/

2019-03-27 22:38:24.848 DEBUG 39802 --- [           main] o.s.boot.SpringApplication               : Loading source class com.example.exitcode.DemoApplication
2019-03-27 22:38:24.915 DEBUG 39802 --- [           main] o.s.b.c.c.ConfigFileApplicationListener  : Loaded config file "file:./config/application.properties" (file:./config/application.properties)
2019-03-27 22:38:24.915 DEBUG 39802 --- [           main] o.s.b.c.c.ConfigFileApplicationListener  : Loaded config file "file:./application.properties" (file:./application.properties)
2019-03-27 22:38:24.915 DEBUG 39802 --- [           main] o.s.b.c.c.ConfigFileApplicationListener  : Loaded config file "jar:file:xxxxx-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/config/application.properties" (classpath:/config/application.properties)
2019-03-27 22:38:24.915 DEBUG 39802 --- [           main] o.s.b.c.c.ConfigFileApplicationListener  : Loaded config file "jar:file:xxxxx-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/application.properties" (classpath:/application.properties)
2.2 属性值怎么取

优先级高的会覆盖优先级低的。

./config/application.properties

testconfig.first=./config/
#testconfig.second=./config/
#testconfig.third=./config/
#testconfig.fourth=./config/

./application.properties

testconfig.first=./
testconfig.second=./
#testconfig.third=./
#testconfig.fourth=./

classpath:/config/application.properties

testconfig.first=classpath/config/
testconfig.second=classpath/config/
testconfig.third=classpath/config/
#testconfig.fourth=classpath/config/

classpath:/application.properties

testconfig.first=classpath
testconfig.second=classpath
testconfig.third=classpath
testconfig.fourth=classpath

输出如下:

2019-03-27 23:29:12.434  INFO 1335 --- [           main] com.example.properties.DemoApplication   : No active profile set, falling back to default profiles: default
first: ./config/
second: ./
third: classpath/config/
fourth: classpath
2019-03-27 23:29:13.052  INFO 1335 --- [           main] com.example.properties.DemoApplication   : Started DemoApplication in 16.565 seconds (JVM running for 23.467)
2.3 多环境配置文件

加一个文件: classpath:/application-product.properties

testconfig.first=product-classpath
testconfig.second=product-classpath

通过 spring.profiles.active 来指定环境所对应的 properties 文件:
运行 java -jar build/libs/properties-0.0.1-SNAPSHOT.jar --spring.profiles.active=product, 输出如下:

2019-03-28 20:34:44.726  INFO 25859 --- [           main] com.example.properties.DemoApplication   : The following profiles are active: product
first: product-classpath
second: product-classpath
third: classpath/config/
fourth: classpath
fifth: ./config/
sixth: ./config/
seventh: ./config/
eightth: ./config/
2.3 使用 yaml 文件来代替 properties 文件。

也可以使用 yaml 格式的文件。但是在同等目录下,properties 优先级高于 yaml 文件的配置信息。

新增文件 ./config/application.yml

testconfig:
  frist: ./config/yml
  second: ./config/yml

命令 java -jar build/libs/properties-0.0.1-SNAPSHOT.jar 输出为:

first: ./config/
second: ./config/yml
third: classpath/config/
fourth: classpath
fifth: ./config/
sixth: ./config/
seventh: ./config/
eightth: ./config/
2.5 属性文件中可以使用变量已经声明过的变量值:
app.name=MyApp
app.description=${app.name} is a Spring Boot application

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/73999.html

相关文章

  • 吐血整理 20 道 Spring Boot 面试题,我经常拿来面试别人!

    摘要:你如何理解中的可以理解为启动器,它包含了一系列可以集成到应用里面的依赖包,你可以一站式集成及其他技术,而不需要到处找示例代码和依赖包。如你想使用访问数据库,只要加入启动器依赖就能使用了。 面试了一些人,简历上都说自己熟悉 Spring Boot, 或者说正在学习 Spring Boot,一问他们时,都只停留在简单的使用阶段,很多东西都不清楚,也让我对面试者大失所望。 下面,我给大家总结...

    haoguo 评论0 收藏0
  • 最渣的 Spring Boot 文章

    摘要:如删除临时文件,清除缓存信息,读取配置文件信息,数据库连接等。提供的接口也可以满足该业务场景。不同点中方法的参数为,而接口中方法的参数为数组。 spring-boot-starter-parent Maven的用户可以通过继承spring-boot-starter-parent项目来获得一些合理的默认配置。这个parent提供了以下特性: 默认使用Java 8 使用UTF-8编码 一...

    yanest 评论0 收藏0
  • Spring Boot(二)Spring Boot配置

    摘要:不同的环境之间的配置存在覆盖关系。提供了一种统一的方式来管理应用的配置,允许开发人员使用属性文件文件环境变量和命令行参数来定义优先级不同的配置值。比如命令行参数的优先级被设置为最高。 一.关于Spring Boot的配置 Spring Boot 对于开发人员最大的好处在于可以对 Spring 应用进行自动配置。Spring Boot 会根据应用中声明的第三方依赖来自动配置 Spring...

    nicercode 评论0 收藏0
  • Spring Boot 配置文件中的花样,看这一篇足矣!

    摘要:的默认配置文件位置为。比如,我们需要自定义模块的服务端口号,可以在中添加来指定服务端口为,也可以通过来指定应用名该名字在应用中会被注册为服务名。同时,配置内容都对开发人员可见,本身这也是一种安全隐患。 在快速入门一节中,我们轻松的实现了一个简单的RESTful API应用,体验了一下Spring Boot给我们带来的诸多优点,我们用非常少的代码量就成功的实现了一个Web应用,这是传统的...

    pingan8787 评论0 收藏0
  • spring cloud config将配置存储在数据库中

    摘要:工程描述端口,从数据库中读取配置端口,从读取配置搭建工程创建工程,在工程的文件引入的起步依赖,的连接器,的起步依赖,代码如下在工程的配置文件下做以下的配置其中,为读取的配置文件名,从数据库中读取,必须为。 转载请标明出处: https://blog.csdn.net/forezp/...本文出自方志朋的博客 Spring Cloud Config Server最常见是将配置文件放在本...

    RobinQu 评论0 收藏0

发表评论

0条评论

stormzhang

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<