Spring Cloud (02) - bootstrap file Introduction

bootstrap file description

bootstrap the Chinese translation is "bootstrap" means. In Spring ApplicationContext there will be parent-child relationship. Spring Cloud initialization ApplicationContext called bootstrap ApplicationContext, after a number of sub ApplicationContext created. If the bootstrap ApplicationContext properties may receive some external control of the initialization process, then the corresponding property on or disposed bootstrap.yml bootstrap.properties file. The default will look for them in the root path of Classpath or config path. ApplicationContext is parent-child relationship, and that relying on the ApplicationContext of Environment is also equivalent to an indirect relationship with the father and son, so the definition in bootstrap.yml file property values can also be used by our own definition of bean. But the value inside it will be overwritten values for the same property we define in application.yml in. bootstrap ApplicationContext to find the name of an external properties file is not necessarily bootstrap, it can through the system attribute spring.cloud.bootstrap.nameis specified, such as through the system attribute spring.cloud.bootstrap.namespecifies the name of bootstrap properties file that application, namely in the Classpath will look application.yml root directory or a directory or config application.properties file. System property can also spring.cloud.bootstrap.locationspecify the location of the bootstrap file, their usage is similar Spring Boot specified in the configuration file spring.config.nameand spring.config.location`.

If you specify in bootstrap.yml in spring.profiles.active=dev, you will find the bootstrap ApplicationContext bootstrap-dev.yml file to find the configuration file.

If necessary we also need to do some things in bootstrap ApplicationContext, such as the definition of some of the bean and so on, then we can define a use @Configurationmarked Class, and then in the Classpath META-INF/spring.factoriesby file org.springframework.cloud.bootstrap.BootstrapConfigurationattribute specifies the @Configurationname of the class. The following example of such a @Configurationclass.

@Configuration
public class BootstrapConfiguration {

  @Bean
  public TestService testService() {
    return new TestService();
  }

}

Then the spring.factoriesneed for such definition file.

org.springframework.cloud.bootstrap.BootstrapConfiguration=com.elim.learn.spring.cloud.config.client.BootstrapConfiguration

(Note: This article is written based on Spring Cloud Finchley.SR1)

Guess you like

Origin elim.iteye.com/blog/2442073