SpringBoot yml配置null坑

  • 場景:yml配置變量值是null 解析得到的是" "
  • tip: 這篇最好結(jié)合 MBP中yml屬性注入及邏輯刪除過程一起看下

  • 下面就來分析下原因:
  1. yml解析過程中進行了默認處理
    分析這部分代碼一定要搞清楚Iterator邏輯 因為內(nèi)部增強for默認使用Iterator遍歷的
    最好知悉Iterator及Iterable 兩者區(qū)別
  2. 設(shè)置yml屬性值時進行了默認處理(@Value是通過setter操作的 還是是直接反射字段賦值的)-反射字段賦值的

結(jié)果是: 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 打個斷點 觀察具體的調(diào)用棧信息


具體的調(diào)用棧信息
    1. @Value注解賦值的過程部分流程如下:


      @Value注入的核心代碼

結(jié)論:@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<String> 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);
            }
        }
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末初厚,一起剝皮案震驚了整個濱河市睦尽,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌燥筷,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,734評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異姚淆,居然都是意外死亡兽愤,警方通過查閱死者的電腦和手機彼念,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來浅萧,“玉大人逐沙,你說我怎么就攤上這事⊥莩” “怎么了吩案?”我有些...
    開封第一講書人閱讀 164,133評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長帝簇。 經(jīng)常有香客問我徘郭,道長,這世上最難降的妖魔是什么丧肴? 我笑而不...
    開封第一講書人閱讀 58,532評論 1 293
  • 正文 為了忘掉前任残揉,我火速辦了婚禮,結(jié)果婚禮上芋浮,老公的妹妹穿的比我還像新娘冲甘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,585評論 6 392
  • 文/花漫 我一把揭開白布江醇。 她就那樣靜靜地躺著濒憋,像睡著了一般。 火紅的嫁衣襯著肌膚如雪陶夜。 梳的紋絲不亂的頭發(fā)上凛驮,一...
    開封第一講書人閱讀 51,462評論 1 302
  • 那天,我揣著相機與錄音条辟,去河邊找鬼黔夭。 笑死,一個胖子當(dāng)著我的面吹牛羽嫡,可吹牛的內(nèi)容都是我干的本姥。 我是一名探鬼主播,決...
    沈念sama閱讀 40,262評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼杭棵,長吁一口氣:“原來是場噩夢啊……” “哼婚惫!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起魂爪,我...
    開封第一講書人閱讀 39,153評論 0 276
  • 序言:老撾萬榮一對情侶失蹤先舷,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后滓侍,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蒋川,經(jīng)...
    沈念sama閱讀 45,587評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,792評論 3 336
  • 正文 我和宋清朗相戀三年撩笆,在試婚紗的時候發(fā)現(xiàn)自己被綠了捺球。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,919評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡夕冲,死狀恐怖懒构,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情耘擂,我是刑警寧澤,帶...
    沈念sama閱讀 35,635評論 5 345
  • 正文 年R本政府宣布絮姆,位于F島的核電站醉冤,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏篙悯。R本人自食惡果不足惜蚁阳,卻給世界環(huán)境...
    茶點故事閱讀 41,237評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望鸽照。 院中可真熱鬧螺捐,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至澜沟,卻和暖如春灾票,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背茫虽。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評論 1 269
  • 我被黑心中介騙來泰國打工刊苍, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人濒析。 一個月前我還...
    沈念sama閱讀 48,048評論 3 370
  • 正文 我出身青樓正什,卻偏偏與公主長得像,于是被迫代替她去往敵國和親号杏。 傳聞我的和親對象是個殘疾皇子婴氮,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,864評論 2 354

推薦閱讀更多精彩內(nèi)容