Spring Boot Environment的初始化和预处理

Posted 昵称有毒

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot Environment的初始化和预处理相关的知识,希望对你有一定的参考价值。

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 = {[email protected]} "SimpleCommandLinePropertySource {name=‘commandLineArgs‘}"    // SpringApplication.run(Object source, String... args)
1 = {[email protected]} "StubPropertySource {name=‘servletConfigInitParams‘}"       // web环境特有
2 = {[email protected]} "StubPropertySource {name=‘servletContextInitParams‘}"      // web环境特有
3 = {[email protected]} "MapPropertySource {name=‘systemProperties‘}"                              // JVM参数和Java命令行参数: System.getProperties()
4 = {[email protected]} "SystemEnvironmentPropertySource {name=‘systemEnvironment‘}"  // 系统环境变量: System.getenv()

还有两个是通过

listeners.environmentPrepared(environment);

加载的:

5 = {[email protected]} "[email protected] {name=‘random‘, [email protected]}"
6 = {[email protected]} "[email protected] {name=‘applicationConfigurationProperties‘, properties=[[email protected]5466 {name=‘applicationConfig: [profile=]‘, properties=[[email protected] {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的初始化和预处理的主要内容,如果未能解决你的问题,请参考以下文章

(005)Spring Boot之读取配置文件

Spring Boot 集成测试:模拟环境接口

spring boot读取配置的三种方式

Spring boot- Spring Boot特性2

idea spring boot

Spring Boot加密属性文件数据