美文网首页
基于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 这样来转换,还是说这是一个特例。也许我做得不是很完美,如果有其他更好的方法,请在下面留言告诉我。

相关文章

  • 通过snakeyaml解析yaml文件

    scala 读取yaml 配置文件 配置maven文件pom.xml 创建yaml 文件config.yaml ...

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

    背景:近日在复习Spring的时候开始从头构建一个项目,就打算从XML配置开始(虽然现在都流行SpringBoot...

  • Spring——properties

    spring 配置文件xml文件读取properties文件属性值 spring 代码文件读取properties...

  • 注册bean

    使用步骤 加载配置文件 对于Spring来说,xml,properties,yaml,包括class文件都是配置文...

  • Spring XML 配置

    Spring XML 配置 对于基于XML的配置,Sprig 1.0 的配置文件采用 DTD 格式,Spring ...

  • Spring框架的学习(第二天)

    xml文件导入其他xml文件配置 如果我们在spring框架中配置了多个xml文件,我们可以在读取配置文件的时候把...

  • Spring XML、注解、Java类 配置

    配置Spring一般包括3中方式 基于XML的配置文件 基于注解的配置 基于Java的配置 基于XML的配置 这个...

  • python配置

    python读取配置文件方式(ini、yaml、xml) - 云+社区 - 腾讯云 (tencent.com)[h...

  • Spring中的Bean的配置形式

    Spring中Bean的配置形式有两种,基于XML文件的方式和基于注解的方式。 1.基于XML文件的方式配置Bea...

  • spring Quartz基于配置文件和注解的实现

    一、基于配置文件的实现 ①编写需要调度的类 ②设置配置文件spring-quartz.xml ③启动spring容...

网友评论

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

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