Spring Boot Environment的初始化和预处理
Posted 昵称有毒
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot Environment的初始化和预处理相关的知识,希望对你有一定的参考价值。
Spring Boot Environment的初始化和预处理实在启动时完成的, 即SpringApplication的run方法中。
ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments);
Environment负责读取系统环境和参数信息, 包括但不限于以下几种属性源:
- 系统环境变量: System.getenv()
- JVM参数和Java命令行参数: System.getProperties()
- 程序参数, SpringApplication.run(Object source, String... args)
- 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}}]}]}