application.yml vs bootstrap.yml

2017-12-02  本文已影响25人  b470b9fc7145

What is the diference between putting a property on application.yml or bootstrap.yml in spring boot?

答案一

Bootstrap.yml (bootstrap.properties) is loaded before application.yml (application.properties),it's like application.yml but for the bootstrap phase of an application context. It is typically used for the case "when using Spring Cloud Config Server, you should specify spring.application.name and spring.cloud.config.server.git.uri inside bootstrap.yml", and also some encryption/decryption information.Technically, bootstrap.yml is loaded by a parent Spring ApplicationContext. That parent ApplicationContext is loaded before the one that uses application.yml.

For example, when using Spring Cloud, the 'real' configuration data is usually loaded from a server. In order to get the URL (and other connection configuration, such as passwords, etc.), you need an earlier or "bootstrap" configuration. Thus, you put the config server attributes in the bootstrap.yml, which is used to load the real configuration data (which generally overrides what's in an application.yml [if present]).

翻译

Bootstrap.yml(bootstrap.properties)在application.yml(application.properties)之前加载,就像application.yml,但是用于应用程序上下文的引导阶段。它通常用于“在使用Spring Cloud Config Server时,应该在bootstrap.yml中指定spring.application.name和spring.cloud.config.server.git.uri”以及一些加密/解密信息。技术上,bootstrap.yml由父级Spring ApplicationContext加载。那个父ApplicationContext加载在使用application.yml之前。

例如,使用Spring Cloud时,通常从服务器加载“真实”配置数据。为了获得URL(以及其他连接配置,例如密码等),您需要更早或“引导”配置。因此,你把配置服务器的属性放在bootstrap.yml中,用来加载实际配置数据(通常覆盖application.yml中的内容)。

答案二

I have just asked the Spring Cloud guys and thought I should share the info I have here.

bootstrap.yml is loaded before application.yml.

It is typically used for the following:

Technically, bootstrap.yml is loaded by a parent Spring ApplicationContext. That parent ApplicationContext is loaded before the one that uses application.yml.

翻译
从技术上讲,bootstrap.yml是由父级的Spring ApplicationContext加载的。那个父ApplicationContext加载在使用application.yml之前。

总结

  1. 使用bootstrap用于拉取springCloud服务器端的配置
  2. bootstrap在application之前就会被加载;意思是没有发现bootstrap才会去找application;配置先找bootstrap中,没有再找application中

参考文档

  1. What is the diference between putting a property on application.yml or bootstrap.yml in spring boot?
  2. application.yml vs bootstrap.yml in spring boot
上一篇下一篇

猜你喜欢

热点阅读