SpringBoot yml配置null坑

  • 场景:yml配置变量值是null 解析得到的是" "
  • tip: 这篇最好结合 MBP中yml属性注入及逻辑删除过程一起看下

  • 下面就来分析下原因:
  1. yml解析过程中进行了默认处理
    分析这部分代码一定要搞清楚Iterator逻辑 因为内部增强for默认使用Iterator遍历的
    最好知悉Iterator及Iterable 两者区别
  2. 设置yml属性值时进行了默认处理(@Value是通过setter操作的 还是是直接反射字段赋值的)-反射字段赋值的

结果是: 1
org.springframework.boot.env.OriginTrackedYamlLoader.OriginTrackingConstructor#getValue:

        private Object getValue(Object value) {
            return (value != null) ? value : "";
        }

null:


null

“null”:


"null"

    1. yml|yaml解析及默认处理的部分流程如下:
  1. org.springframework.boot.SpringApplication#run(java.lang.String...)
  2. org.springframework.boot.SpringApplication#prepareEnvironment
  3. org.springframework.boot.SpringApplicationRunListeners#environmentPrepared
  4. org.springframework.boot.context.event.EventPublishingRunListener#environmentPrepared
  5. org.springframework.context.event.SimpleApplicationEventMulticaster#multicastEvent(org.springframework.context.ApplicationEvent)
  6. org.springframework.context.event.SimpleApplicationEventMulticaster#multicastEvent(org.springframework.context.ApplicationEvent, org.springframework.core.ResolvableType)
  7. org.springframework.context.event.SimpleApplicationEventMulticaster#invokeListener
  8. org.springframework.context.event.SimpleApplicationEventMulticaster#doInvokeListener
  9. org.springframework.boot.context.config.ConfigFileApplicationListener#onApplicationEvent
  10. org.springframework.boot.context.config.ConfigFileApplicationListener#onApplicationEnvironmentPreparedEvent
  11. org.springframework.boot.context.config.ConfigFileApplicationListener#postProcessEnvironment
  12. org.springframework.boot.context.config.ConfigFileApplicationListener#addPropertySources
  13. org.springframework.boot.context.config.ConfigFileApplicationListener.Loader#load()
  14. org.springframework.boot.context.config.FilteredPropertySource#apply
  15. org.springframework.boot.context.config.ConfigFileApplicationListener.Loader#load()
  16. org.springframework.boot.context.config.ConfigFileApplicationListener.Loader#load(org.springframework.boot.context.config.ConfigFileApplicationListener.Profile, org.springframework.boot.context.config.ConfigFileApplicationListener.DocumentFilterFactory, org.springframework.boot.context.config.ConfigFileApplicationListener.DocumentConsumer)
  17. org.springframework.boot.context.config.ConfigFileApplicationListener.Loader#load(java.lang.String, java.lang.String, org.springframework.boot.context.config.ConfigFileApplicationListener.Profile, org.springframework.boot.context.config.ConfigFileApplicationListener.DocumentFilterFactory, org.springframework.boot.context.config.ConfigFileApplicationListener.DocumentConsumer)
  18. org.springframework.boot.context.config.ConfigFileApplicationListener.Loader#loadForFileExtension
  19. org.springframework.boot.context.config.ConfigFileApplicationListener.Loader#load(org.springframework.boot.env.PropertySourceLoader, java.lang.String, org.springframework.boot.context.config.ConfigFileApplicationListener.Profile, org.springframework.boot.context.config.ConfigFileApplicationListener.DocumentFilter, org.springframework.boot.context.config.ConfigFileApplicationListener.DocumentConsumer)
  20. org.springframework.boot.context.config.ConfigFileApplicationListener.Loader#loadDocuments
  21. org.springframework.boot.env.YamlPropertySourceLoader#load
  22. org.springframework.boot.env.OriginTrackedYamlLoader#load
  23. org.springframework.beans.factory.config.YamlProcessor#process(org.springframework.beans.factory.config.YamlProcessor.MatchCallback)
  24. org.springframework.beans.factory.config.YamlProcessor#process(org.springframework.beans.factory.config.YamlProcessor.MatchCallback, org.yaml.snakeyaml.Yaml, org.springframework.core.io.Resource)
  25. java.util.Iterator#next
  26. org.yaml.snakeyaml.constructor.BaseConstructor#getData
  27. org.yaml.snakeyaml.constructor.BaseConstructor#constructDocument
  28. org.springframework.boot.env.OriginTrackedYamlLoader.OriginTrackingConstructor#constructObject
  29. org.yaml.snakeyaml.constructor.BaseConstructor#constructObject
  30. org.yaml.snakeyaml.constructor.BaseConstructor#constructObjectNoCheck
  31. org.yaml.snakeyaml.constructor.SafeConstructor.ConstructYamlMap#construct
  32. org.yaml.snakeyaml.constructor.BaseConstructor#constructMapping
  33. org.yaml.snakeyaml.constructor.SafeConstructor#constructMapping2ndStep
  34. org.yaml.snakeyaml.constructor.BaseConstructor#constructMapping2ndStep
  35. org.springframework.boot.env.OriginTrackedYamlLoader.OriginTrackingConstructor#constructObject
  36. org.yaml.snakeyaml.constructor.BaseConstructor#constructObject
    ....

在org.springframework.boot.env.OriginTrackedYamlLoader.OriginTrackingConstructor#getValue 打个断点 观察具体的调用栈信息


具体的调用栈信息
    1. @Value注解赋值的过程部分流程如下:


      @Value注入的核心代码

结论:@Value是通过反射字段赋值的 并没有进行null的处理操作
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.AutowiredFieldElement#inject

@Override
        protected void inject(Object bean, @Nullable String beanName, @Nullable PropertyValues pvs) throws Throwable {
            Field field = (Field) this.member;
            Object value;
            if (this.cached) {
                value = resolvedCachedArgument(beanName, this.cachedFieldValue);
            }
            else {
                DependencyDescriptor desc = new DependencyDescriptor(field, this.required);
                desc.setContainingClass(bean.getClass());
                Set autowiredBeanNames = new LinkedHashSet<>(1);
                Assert.state(beanFactory != null, "No BeanFactory available");
                TypeConverter typeConverter = beanFactory.getTypeConverter();
                try {
                    value = beanFactory.resolveDependency(desc, beanName, autowiredBeanNames, typeConverter);
                }
                catch (BeansException ex) {
                    throw new UnsatisfiedDependencyException(null, beanName, new InjectionPoint(field), ex);
                }
                synchronized (this) {
                    if (!this.cached) {
                        if (value != null || this.required) {
                            this.cachedFieldValue = desc;
                            registerDependentBeans(beanName, autowiredBeanNames);
                            if (autowiredBeanNames.size() == 1) {
                                String autowiredBeanName = autowiredBeanNames.iterator().next();
                                if (beanFactory.containsBean(autowiredBeanName) &&
                                        beanFactory.isTypeMatch(autowiredBeanName, field.getType())) {
                                    this.cachedFieldValue = new ShortcutDependencyDescriptor(
                                            desc, autowiredBeanName, field.getType());
                                }
                            }
                        }
                        else {
                            this.cachedFieldValue = null;
                        }
                        this.cached = true;
                    }
                }
            }
            if (value != null) {
                ReflectionUtils.makeAccessible(field);
                field.set(bean, value);
            }
        }
    }

你可能感兴趣的:(SpringBoot yml配置null坑)