今天在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;
@Test
public 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.PropertyPlaceholderConfi
而这个基于命名空间的配置,其实内部就是创建一个PropertyPlaceholderConfi
PropertyPlaceholderConfi