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

前端 未结 8 780
慢半拍i
慢半拍i 2020-12-02 03:13

What is the difference between putting a property on application.yml or bootstrap.yml in spring boot? In logging.config case, the application works different.

相关标签:
8条回答
  • 2020-12-02 04:14

    Well, I totally agree with answers already exist on this point:

    • bootstrap.yml is used to save parameters that point out where the remote configuration is and Bootstrap Application Context is created with these remote configuration.

    Actually, it is also able to store normal properties just the same as what application.yml do. But pay attention on this tricky thing:

    • If you do place properties in bootstrap.yml, they will get lower precedence than almost any other property sources, including application.yml. As described here.

    Let's make it clear, there are two kinds of properties related to bootstrap.yml:

    • Properties that are loaded during the bootstrap phase. We use bootstrap.yml to find the properties holder (A file system, git repository or something else), and the properties we get in this way are with high precedence, so they cannot be overridden by local configuration. As described here.
    • Properties that are in the bootstrap.yml. As explained early, they will get lower precedence. Use them to set defaults maybe a good idea.

    So the differences between putting a property on application.yml or bootstrap.yml in spring boot are:

    • Properties for loading configuration files in bootstrap phase can only be placed in bootstrap.yml.
    • As for all other kinds of properties, place them in application.yml will get higher precedence.
    0 讨论(0)
  • 2020-12-02 04:15

    Another use for bootstrap.yml is to load configuration from kubernetes configmap and secret resources. The application must import the spring-cloud-starter-kubernetes dependency.

    As with the Spring Cloud Config, this has to take place during the bootstrap phrase.

    From the docs :

    spring:
      application:
        name: cloud-k8s-app
      cloud:
        kubernetes:
          config:
            name: default-name
            namespace: default-namespace
            sources:
             # Spring Cloud Kubernetes looks up a ConfigMap named c1 in namespace default-namespace
             - name: c1
    

    So properties stored in the configmap resource with meta.name default-name can be referenced just the same as properties in application.yml

    And the same process applies to secrets :

    spring:
      application:
        name: cloud-k8s-app
      cloud:
        kubernetes:
          secrets:
            name: default-name
            namespace: default-namespace
            sources:
             # Spring Cloud Kubernetes looks up a Secret named s1 in namespace default-namespace
             - name: s1
    
    0 讨论(0)
提交回复
热议问题