@RunWith(SpringJUnit4ClassRunner.class) public class SpringDemo1 { //测试业务层类 @Resource(name="accountService") private AccountService accountService; @Test public void demo1(){ accountService.transfer("aaa","bbb",200d); } } <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 引入外部的属性文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置c3P0连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 配置业务层类 --> <bean id="accountService" class="cn.muke.spring.demo1.AccountServiceImpl"> <property name="accountDao" ref="accountDao"></property> </bean> <!-- 配置DAO类 --> <bean id="accountDao" class="cn.muke.spring.demo1.AccountDaoImpl"> <property name="dataSource" ref="dataSource"></property> </bean> </beans>
没加载配置文件
楼上或许是正确的,jar包和jak版本存在兼容性问题,jdk1.8必须是jar包4以上的版本
看一下你的jdk编译版本,1.8不兼容,改成1.6就ok了,不是系统的,是项目的!右键项目>属性>compiler改成1.6。如果你开发用的1.8,这个时候项目会有小红叉,别管他。要想去掉小红叉,开发也用1.6。jdk1.8太多坑了……
测试类上需要注解 @ContextConfiguration{"application*.xml"}