千家信息网

SpringBoot配置文件是怎样的

发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,本篇文章为大家展示了SpringBoot配置文件是怎样的,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。环境:springboot2.2.13SpringBoo
千家信息网最后更新 2026年11月03日SpringBoot配置文件是怎样的

本篇文章为大家展示了SpringBoot配置文件是怎样的,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

环境:springboot2.2.13

SpringBoot 中有以下两种配置文件

  • bootstrap (.yml 或者 .properties)

  • application (.yml 或者 .properties)

接下来说下这两个配置文件有什么区别!

bootstrap/ application 的区别

bootstrap.yml(bootstrap.properties)先加载

application.yml(application.properties)后加载

bootstrap.yml 用于应用程序上下文的引导阶段,由父Spring ApplicationContext加载。父ApplicationContext 被加载到使用application.yml的之前。

在 Spring Boot 中有两种上下文,一种是 bootstrap, 另外一种是 application, bootstrap 是应用程序的父上下文,也就是说 bootstrap 加载优先于 applicaton。bootstrap 主要用于从额外的资源来加载配置信息,还可以在本地外部配置文件中解密属性。这两个上下文共用一个环境,它是任何Spring应用程序的外部属性的来源。bootstrap 里面的属性会优先加载,它们默认也不能被本地相同配置覆盖也就是说bootstrap中的配置不能被覆盖。

bootstrap/ application 的应用场景

application 配置文件这个容易理解,主要用于 Spring Boot 项目的自动化配置。

bootstrap 配置文件有以下几个应用场景。

  • 使用 Spring Cloud Config 配置中心时,这时需要在 bootstrap 配置文件中添加连接到配置中心的配置属性来加载外部配置中心的配置信息;

  • 一些固定的不能被覆盖的属性

  • 一些加密/解密的场景;

以下是来自官方对bootstrap.[yml/properties]的一个说明:

A Spring Cloud application operates by creating a "bootstrap" context, which is a parent context for the main application. This context is responsible for loading configuration properties from the external sources and for decrypting properties in the local external configuration files. The two contexts share an Environment, which is the source of external properties for any Spring application. By default, bootstrap properties (not bootstrap.properties but properties that are loaded during the bootstrap phase) are added with high precedence, so they cannot be overridden by local configuration.  The bootstrap context uses a different convention for locating external configuration than the main application context. Instead of application.yml (or .properties), you can use bootstrap.yml, keeping the external configuration for bootstrap and main context nicely separate

自定义配置文件

  1. @Configuration

  2. @ConfigurationProperties(prefix = "pack")

  3. @PropertySource(value = "classpath:config.properties")

  4. public class PackProperties {

  5. private String name;

  6. private Integer age ;

  7. }


pack.name=xxxx pack.age=10

注意:@PropertySource只能加载properties文件不能加载yml文件。

导入Spring XML文件

@Configuration @ImportResource(locations = {"classpath:application-jdbc.xml", "classpath:application-aop.xml"}) public class ResourceConfig { }

application-jdbc.xml,application-aop.xml两个配置文件必须是spring的配置文件。

配置文件默认值

有如下代码:

@Value("${pack.name}") private String name ;

当配置文件中没有配置pack.name时,这时候服务启动会报错,这时候我们可以通过设置默认值来防止错误。

@Value("${pack.name:xx}")     private String name ;

这里冒号 ":" 后面就是设置的默认值,这里也可以什么也不写${pack.name:}。

加载制定环境的配置文件

一般我们都会为测试环境,开发环境,生产环境分别设置不同的配置文件,不同的环境加载不同的配置文件。

现有如下配置文件:

生成环境加载prod文件,测试环境加载test文件,开发环境加载dev文件,只需在application.yml配置文件中加入如下配置。

spring:   profiles:     active:     - dev

这是在配置文件中写死,我们也可以在命令行制定

java -jar xxxxx.jar --spring.profiles.active=dev

通过include包含多个配置文件,include是与环境无关的(也就是不管是什么环境都会加载)

spring:   profiles:     active:     - dev     include:     - cloud     - secret

配置文件加载顺序

查看

ConfigFileApplicationListener.java源码,该文件中定义了从哪些地方加载配置文件。

加载顺序:

  • file:./config/

  • file:./config/*/

  • file:./

  • classpath:config/

  • classpath:

优先级从高到低,高的覆盖低的。

默认情况下加载的是application.yml或application.properties文件。

在启动应用程序的时候可以制定配置文件的名称

java -jar xxxxx.jar --spring.config.name=myapp

源码:

通过代码读取配置文件

@SpringBootApplication public class SpringBootJwtApplication implements ApplicationRunner{      public static void main(String[] args) {         SpringApplication.run(SpringBootJwtApplication.class, args);     }     @Override     public void run(ApplicationArguments args) throws Exception {         ClassPathResource resource = new ClassPathResource("config.properties");             try {                  Properties properties = PropertiesLoaderUtils.loadProperties(resource);                       System.out.println(properties) ;                      } catch (IOException e) {                     e.printStackTrace() ;         }     } }

这里通过PropertiesLoaderUtils工具类来加载资源

上述内容就是SpringBoot配置文件是怎样的,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。

配置 文件 环境 应用 属性 上下 上下文 应用程序 程序 不同 两个 也就是 场景 也就是说 代码 信息 内容 就是 技能 源码 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 什么是科技互联网 华服互联网科技靠谱吗 伟宏互联网科技 广州百单互联网科技有限公司 互联网通讯科技 互联网科技资迅 奇酷互联网络科技 广州互联网络科技有限公司 腾讯科技 互联网女皇 易通互联网络科技有限公司 四个字互联网科技公司取名 华脉科技工业互联网产品 互联网和高科技公司 青岛科技大学互联网大赛研究生 晓花互联网科技有限公司网址 广州市恰铎互联网科技有限公司 阿里互联网科技企业 九思连强互联网电子科技有限公司 互联网硬科技公司 广州全民伙伴互联网科技有限公司 互联网科技园 广州万易互联网科技有限公司 汇久创客互联网科技 北京大学互联网科技怎么样 浙南科技城互联网大厦建设工程 翼展科技 首家互联网医院 互联网金融科技名词解释 广州智享互联网科技 长沙渺峰互联网科技有限公司 福仕德教育科技互联网
0