spring 的数据源配置出错Error creating bean with name 'dataSource' defined

来源:互联网 发布:手机淘宝开店教程视频 编辑:程序博客网 时间:2024/06/08 09:39

如果spring配置是使用这种方式传递的properties文件
这里是直接以容器来加载文件
但是username这个属性已经存在在容器中和配置文件中的属性冲突了

所以在配置数据源的时候
传递给username的value使用${username}得不到正确的值

<context:property-placeholder location="classpath:properties/dbconfig.properties" />
<bean id="dataSource" class="..." init-method="init" destroy-method="close">        <property name="username" value="${username}" ><bean>

解决:

1.在db.properties将username属性修改一下

uname: root

然后在spring配置的dataSource里修改属性值:

<property name="username" value="${uname}" >

2.修改spring配置的

<context:property-placeholder location="classpath:properties/dbconfig.properties" />

替换,
以这种方式引入配置文件
是以配置第三方bean的方式配置属性,所以这个bean中的属性是不会和容器中的属性冲突的

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">      <!-- PropertyPlaceholderConfigurer类中有个locations属性,接收的是一个数组,即我们可以在下面配好多个properties文件 -->      <property name="location" value="classpath:properties/dbconfig.properties" />    </bean>

这里记录下…

阅读全文
0 0