1. 程式人生 > >Spring Boot Environment的初始化和預處理

Spring Boot Environment的初始化和預處理

bst div 方法 ams jdb ram rep ntp table

Spring Boot Environment的初始化和預處理實在啟動時完成的, 即SpringApplication的run方法中。

ConfigurableEnvironment environment = prepareEnvironment(listeners,
                    applicationArguments);

Environment負責讀取系統環境和參數信息, 包括但不限於以下幾種屬性源:

  1. 系統環境變量: System.getenv()
  2. JVM參數和Java命令行參數: System.getProperties()
  3. 程序參數, SpringApplication.run(Object source, String... args
    )
  4. WEB環境下, 還會讀取context-parameter

這些信息保存在全局變量:(見AbstractEnvironment)

private final MutablePropertySources propertySources

在web環境中,加載的屬性源列表:

0 = {SimpleCommandLinePropertySource@1223} "SimpleCommandLinePropertySource {name=‘commandLineArgs‘}"    // SpringApplication.run(Object source, String... args)
1 = {PropertySource$StubPropertySource@1224} "StubPropertySource {name=‘servletConfigInitParams‘}"       // web環境特有
2 = {PropertySource$StubPropertySource@1225} "StubPropertySource {name=‘servletContextInitParams‘}"      // web環境特有
3 = {MapPropertySource@1226} "MapPropertySource {name=‘systemProperties‘}"                              // JVM參數和Java命令行參數: System.getProperties()
4 = {SystemEnvironmentPropertySource@1227} "SystemEnvironmentPropertySource {name=‘systemEnvironment‘}"  // 系統環境變量: System.getenv()

還有兩個是通過

listeners.environmentPrepared(environment);

加載的:

5 = {RandomValuePropertySource@1693} "RandomValuePropertySource@735937428 {name=‘random‘, properties=java.util.Random@5f9edf14}"
6 = {ConfigFileApplicationListener$ConfigurationPropertySources@1694} "ConfigurationPropertySources@1752461090 {name=‘applicationConfigurationProperties‘, properties=[EnumerableCompositePropertySource@788625466 {name=‘applicationConfig: [profile=]‘, properties=[MapPropertySource@1753714541 {name=‘applicationConfig: [classpath:/application.yml]‘, properties={spring.application.name=example, spring.jackson.default-property-inclusion=non_null, spring.jpa.open-in-view=false, spring.jpa.hibernate.ddl-auto=none, spring.jpa.database=MYSQL, spring.jpa.show-sql=true, spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect, spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect, spring.aop.proxy-target-class=true, alibaba.druid.url=jdbc:mysql://localhost:3306/hotel?useUnicode=true&characterEncoding=utf-8, alibaba.druid.driver-class-name=com.mysql.jdbc.Driver, alibaba.druid.username=root, alibaba.druid.password=, logging.level.com.hotel=debug, logging.level.org.hibernate.SQL=debug, logging.level.org.springframework=debug, logging.file=hotel.log}}]}]}

Spring Boot Environment的初始化和預處理