死磕源码系列【SystemEnvironmentPropertySourceEnvironmentPostProcessor、SpringApplicationJsonEnvironmentPostP

女爷i 2022-11-20 08:20 287阅读 0赞

SystemEnvironmentPropertySourceEnvironmentPostProcessor类是EnvironmentPostProcessor接口的实现类,用来替换Environment环境中的systemEnvironment,systemEnvironment中最初存储的是SystemEnvironmentPropertySource类,现由实现类OriginAwareSystemEnvironmentPropertySource替换,OriginAwareSystemEnvironmentPropertySource提供了获取Origin的方法,即返回SystemEnvironmentOrigin对象。

先看下SystemEnvironmentPropertySource类中存储的系统环境配置:

我们可以看到里面存储的都是操作系统相关的环境变量配置

SystemEnvironmentPropertySourceEnvironmentPostProcessor源码:
  1. public class SystemEnvironmentPropertySourceEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered {
  2. /** * 后置处理器的优先级,其优先级高于SpringApplicationJsonEnvironmentPostProcessor * 值越小优先级越高 */
  3. public static final int DEFAULT_ORDER = SpringApplicationJsonEnvironmentPostProcessor.DEFAULT_ORDER - 1;
  4. private int order = DEFAULT_ORDER;
  5. //后置处理器回调方法
  6. @Override
  7. public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
  8. //获取系统环境配置名,即:systemEnvironment
  9. String sourceName = StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME;
  10. //获取环境对象中的系统配置
  11. PropertySource<?> propertySource = environment.getPropertySources().get(sourceName);
  12. if (propertySource != null) {
  13. //替换系统环境对象中的环境配置
  14. replacePropertySource(environment, sourceName, propertySource);
  15. }
  16. }
  17. @SuppressWarnings("unchecked")
  18. private void replacePropertySource(ConfigurableEnvironment environment, String sourceName,
  19. PropertySource<?> propertySource) {
  20. //获取环境配置,转换为字典结构
  21. Map<String, Object> originalSource = (Map<String, Object>) propertySource.getSource();
  22. //转换为将要替换的环境对象
  23. SystemEnvironmentPropertySource source = new OriginAwareSystemEnvironmentPropertySource(sourceName,
  24. originalSource);
  25. //替换环境对象
  26. environment.getPropertySources().replace(sourceName, source);
  27. }
  28. @Override
  29. public int getOrder() {
  30. return this.order;
  31. }
  32. public void setOrder(int order) {
  33. this.order = order;
  34. }
  35. /** * 为SystemEnvironmentPropertySource提供SystemEnvironmentOrigin源 * 其Origin代表的是属性配置最原始的源相关信息 */
  36. protected static class OriginAwareSystemEnvironmentPropertySource extends SystemEnvironmentPropertySource
  37. implements OriginLookup<String> {
  38. OriginAwareSystemEnvironmentPropertySource(String name, Map<String, Object> source) {
  39. super(name, source);
  40. }
  41. @Override
  42. public Origin getOrigin(String key) {
  43. String property = resolvePropertyName(key);
  44. if (super.containsProperty(property)) {
  45. return new SystemEnvironmentOrigin(property);
  46. }
  47. return null;
  48. }
  49. }
  50. }

SpringApplicationJsonEnvironmentPostProcessor类也是EnvironmentPostProcessor环境后置处理器函数,其优先级比SystemEnvironmentPropertySourceEnvironmentPostProcessor高,会将系统属性spring.application.json或SPRING_APPLICATION_JSON解析成JSON,并存储到Environment:

在这里插入图片描述

上述是通过代码的 形式传递spring.application.json参数,也可以通过运行jar包时传递:

  1. java -jar demo-0.0.1-SNAPSHOT.jar --spring.application.json='{"a":"bar","b":23}'
  2. //或
  3. java -Dspring.application.json='{"a":"bar","b":23}' -jar demo-0.0.1-SNAPSHOT.jar

GitHub地址:https://github.com/mingyang66/spring-parent

发表评论

表情:
评论列表 (有 0 条评论,287人围观)

还没有评论,来说两句吧...

相关阅读