关于spring boot项目配置文件的一些想法

Posted 记录技术百态

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于spring boot项目配置文件的一些想法相关的知识,希望对你有一定的参考价值。

一、springboot项目中有两种配置文件

springboot项目中有两种配置文件 bootstrap 和 application

bootstrap是应用程序的父上下文,由父Spring ApplicationContext加载。所以加载顺序优先于application。

bootstrap 里面的属性不能被覆盖。

应用场景

  • bootstrap
  1. 使用 Spring Cloud Config 配置中心时,这时需要在bootstrap 配置文件中添加连接到配置中心的配置属性,来加载外部配置中心的配置信息
  2. 一些固定的不能被覆盖的属性
  3. 一些加密/解密的场景
  • application
  1. 主要用于 Spring Boot 项目的自动化配置

二、配置文件中的一些使用方法

spring:
  application:
    name: @artifactId@
  cloud:
    nacos:
      discovery:
        server-addr: nacoshost:8848
      config:
        server-addr: ${spring.cloud.nacos.discovery.server-addr}
        file-extension: yml
        shared-dataids: application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
  autoconfigure:
    exclude: org.springframework.cloud.gateway.config.GatewayAutoConfiguration,org.springframework.cloud.gateway.config.GatewayClassPathWarningAutoConfiguration
  profiles:
    active: @profiles.active@

这段是配置文件内容:@artifactId@ 可以定位到pom 文件中的 <artifactId>pipe-server</artifactId>

          @profiles.active@ 可以定位到pom文件中的 <profiles.active>test</profiles.active>

server-addr: ${spring.cloud.nacos.discovery.server-addr}
这种写法 ${}定位的值就是本yml文件:server-addr: nacoshost:8848 这个地方

三、外部获取配置文件

粗浅的说 就是把外部链接地址放在bootstrap 文件里面 然后去读取文件 外部文件或者在服务器上,或者在git 通过nacos、spring cloud config、apollo 等等

以上是关于关于spring boot项目配置文件的一些想法的主要内容,如果未能解决你的问题,请参考以下文章