Could not resolve placeholder 'jdbc.driverClassName' in string value "${jdbc.driverClassName}错误

来源:互联网 发布:ext js 学多久 编辑:程序博客网 时间:2024/06/03 16:16

今天在MyEclipse搭建spring框架,通过自动生成spring框架,布好DAO层还有Service层后,还导入了单元测试,没想到在测试时出现了

Could not resolve placeholder 'jdbc.driverClassName' in string value "${jdbc.driverClassName}错误


具体代码如下:

test类

import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import Service.AccountService;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext.xml")public class test {@Resource(name="accountService")private AccountService accountService;@Testpublic void demo1(){accountService.transfer("aaa", "bbb", 200d);}}
service层

<tx:annotation-driven transaction-manager="transactionManager" /><bean id="accountService" class="Service.AccountServiceImpl"></bean>

DAO层

<context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true" /><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driverClass}"/><property name="jdbcUrl" value="${jdbc.url}" /><property name="user" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></bean><bean id="accountDao" class="Dao.AccountDaoImpl">   <property name="dataSource" ref="dataSource"></property></bean>
数据库

jdbc.properties 文件已导入,设置好数据库的键值对、


然后在测试时出现该错误,思考一下,可能是该数据库文件的键名称与DAO层的dataSource不对应,或者是名称打错

开始调试bug-----------------------------------

我先删除了DAO层的第一行

<property name="driverClass" value="${jdbc.driverClass}"/>

然后错误信息就变了

Could not resolve placeholder 'jdbc.driverClassName' in string value "${jdbc.url}  错误

证明不是名称打错,应该是文件jdbc.properties导入失败-------------------------

再次寻找原因

网上说修改一下

<context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true" />
在其后面加上    ignore-unresolvable="true"   属性,测试后还是同样的错误---------------------------------------

再次寻找原因

在网上找到

可能是Spring容器的配置问题

Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的Bean就会停止对剩余PropertyPlaceholderConfigurer的扫描(Spring 3.1已经使用PropertySourcesPlaceholderConfigurer替代PropertyPlaceholderConfigurer了)。 

而这个基于命名空间的配置,其实内部就是创建一个PropertyPlaceholderConfigurer Bean而已。换句话说,即Spring容器仅允许最多定义一个PropertyPlaceholderConfigurer(或),其余的会被Spring忽略掉(其实Spring如果提供一个警告就好了)。 


然后我发现我是自动生成spring框架的spring DSL,然后它在spring_project2-dao-context.xml 也就是DAO层的配置文件上有一堆预先植入的代码,里面可能有

PropertyPlaceholderConfigurer配置,我将其全部删除,然后测试,没有错误,成功了,成功修改数据库数据!


阅读全文
0 0
原创粉丝点击