背景:
近日在復(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)換,還是說這是一個特例。也許我做得不是很完美致扯,如果有其他更好的方法肤寝,請在下面留言告訴我。