美文网首页
基于XML配置的Spring,读取yaml文件

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

作者: 柚子光谱 | 来源:发表于2020-03-27 23:07 被阅读0次

    背景:
    近日在复习Spring的时候开始从头构建一个项目,就打算从XML配置开始(虽然现在都流行SpringBoot,但是核心还是Spring没变)。由于工作习惯,所以使用 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
    

    可以看到没读取到 dataSource 的配置信息。

    在网上几番查找之后有了点眉目,主要是参考了这一条答案:
    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 这样来转换,还是说这是一个特例。也许我做得不是很完美,如果有其他更好的方法,请在下面留言告诉我。

    相关文章

      网友评论

          本文标题:基于XML配置的Spring,读取yaml文件

          本文链接:https://www.haomeiwen.com/subject/bshduhtx.html