基于XML配置的Spring,讀取yaml文件

背景:
近日在復(fù)習(xí)Spring的時候開始從頭構(gòu)建一個項目,就打算從XML配置開始(雖然現(xiàn)在都流行SpringBoot,但是核心還是Spring沒變)。由于工作習(xí)慣,所以使用 xxxx.yaml 來做配置文件。但是在一切都ready的時候卻收到如下報錯:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'jdbc.driver' in value "${jdbc.driver}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
    at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:236)
    at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveStringValue(BeanDefinitionVisitor.java:282)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveValue(BeanDefinitionVisitor.java:204)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitPropertyValues(BeanDefinitionVisitor.java:141)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitBeanDefinition(BeanDefinitionVisitor.java:82)
    at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:220)
    ... 36 more

可以看到?jīng)]讀取到 dataSource 的配置信息。

在網(wǎng)上幾番查找之后有了點眉目,主要是參考了這一條答案:
Read spring yml properties from xml configuration
這條答案指出,需要配置一個bean YamlPropertiesFactoryBean,并指明 yaml 文件的位置,所以我在配置里做了這樣的修改

<bean id="yamlProperties" class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean">
    <property name="resources" value="classpath:jdbc.yaml"/>
</bean>

<context:property-placeholder properties-ref="yamlProperties"/>

但是,但是又來了,又得到一個錯誤 No default constructor found; nested exception is java.lang.NoClassDefFoundError: org/yaml/snakeyaml/constructor/BaseConstructor

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.beans.factory.config.YamlPropertiesFactoryBean]: No default constructor found; nested exception is java.lang.NoClassDefFoundError: org/yaml/snakeyaml/constructor/BaseConstructor
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:85)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1147)
    ... 50 more

那么這個錯誤分析起來就要簡單點了,沒找到 snakeyaml 里的BaseConstructor,那就去引入這個包吧。

直接到 pom.xml 里添加依賴就可以了:

    <!-- https://mvnrepository.com/artifact/org.yaml/snakeyaml -->
    <dependency>
      <groupId>org.yaml</groupId>
      <artifactId>snakeyaml</artifactId>
      <version>1.26</version>
    </dependency>

OK,這樣就搞定咯。
———————————————————————————————
通常大家都是 XML+properties的搭配,我也不太清楚yaml文件確實是需要 YamlPropertiesFactoryBean 這樣來轉(zhuǎn)換,還是說這是一個特例。也許我做得不是很完美,如果有其他更好的方法,請在下面留言告訴我。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容