您的位置:首页 > 产品设计 > UI/UE

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

2017-05-25 22:25 726 查看
今天在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配置,我将其全部删除,然后测试,没有错误,成功了,成功修改数据库数据!
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐