Spring Boot 配置文件 bootstrap vs application 到底有什么区别?

简介: 用过 Spring Boot 的都知道在 Spring Boot 中有以下两种配置文件

用过 Spring Boot 的都知道在 Spring Boot 中有以下两种配置文件


bootstrap (.yml 或者 .properties)

application (.yml 或者 .properties)

为什么会有这两种配置文件呢?大家都清楚它们的区别和具体使用场景吗?


bootstrap/ application 的区别

特意去翻了下 Spring Boot 的官方文档,没有找到关于这两种文件的具体定义,然后再翻了下 Spring Cloud 的官方文档找到了它们的区别。


https://cloud.spring.io/spring-cloud-static/Greenwich.SR1/single/spring-cloud.html#_the_bootstrap_application_context


认真阅读了下文档,原文大概意思是这样。


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


因此,对比 application 配置文件,bootstrap 配置文件具有以下几个特性。


boostrap 由父 ApplicationContext 加载,比 applicaton 优先加载

boostrap 里面的属性不能被覆盖

bootstrap/ application 的应用场景

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


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


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

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

一些加密/解密的场景;

以下这个截图是一个国外网友问了一个 Spring Cloud 工程师得到的回答。

image.png

做过 Spring Cloud 微服务的朋友应该对 bootstrap 的应用十分清楚,我们的微信公众号Java技术栈也有 Spring Cloud 的实战教程,在 Spring 专题中都能看到。


好了,今天的分享就到这里,关注Java技术栈微信公众号,在后台回复:boot,获取栈长整理的更多的 Spring Boot 教程,都是实战干货,以下仅为部分预览。


Spring Boot 读取配置的几种方式

Spring Boot 如何做参数校验?

Spring Boot 最核心的 25 个注解!

Spring Boot 2.x 启动全过程源码分析

Spring Boot 2.x 新特性总结及迁移指南

……


相关文章
|
10月前
|
Java 开发工具 Spring
【Azure Application Insights】为Spring Boot应用集成Application Insight SDK
本文以Java Spring Boot项目为例,详细说明如何集成Azure Application Insights SDK以收集和展示日志。内容包括三步配置:1) 在`pom.xml`中添加依赖项`applicationinsights-runtime-attach`和`applicationinsights-core`;2) 在main函数中调用`ApplicationInsights.attach()`;3) 配置`applicationinsights.json`文件。同时提供问题排查建议及自定义日志方法示例,帮助用户顺利集成并使用Application Insights服务。
294 8
|
网络协议 Java Shell
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
853 7
|
缓存 Java Maven
Spring Boot 启动错误:To display the conditions report re-run your application with ‘debug‘ enable —【已解决】
Spring Boot 启动错误:To display the conditions report re-run your application with ‘debug‘ enable —【已解决】
5033 1
|
Java Spring
Spring Boot Application in default package
Spring Boot Application in default package
|
前端开发 JavaScript Java
基于JavaWeb机票订购系统(含前后台)(Java+spring+jsp+bootstrap+mysql)
基于JavaWeb机票订购系统(含前后台)(Java+spring+jsp+bootstrap+mysql)
262 3
|
XML Java API
Spring Boot中使用集成swagger-bootstrap-ui
Spring Boot中使用集成swagger-bootstrap-ui
500 0
|
Java Spring
Command line is too long. Shorten command line for Application or also for Spring Boot default confi
Command line is too long. Shorten command line for Application or also for Spring Boot default confi
369 1
|
Java 数据库 Maven
基于SSM框架疫情之下社区管理系统(spring+springmvc+mybatis+jsp+jquery+bootstrap)
基于SSM框架疫情之下社区管理系统(spring+springmvc+mybatis+jsp+jquery+bootstrap)
171 0
|
Java 应用服务中间件 Maven
框架的优点(SpringBoot VS Servlet)
框架的优点(SpringBoot VS Servlet)
|
Java Linux Windows
windows解决SpringBoot启动时:APPLICATION FAILED TO START
windows解决SpringBoot启动时:APPLICATION FAILED TO START
905 0