用eclipse照视频抄写老师的代码,测试时侯无法加载bean文件,求解!
代码和老师的完全一样,视频看了好几遍了,测试的时候报错:java.lang.IllegalStateException: Failed to load ApplicationContext
我把 ApplicationContext.xml文件中定义的bean注释掉,
<?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="com.demo1.AccountServiceImpl">
<property name="accountDAO" ref="accountDAO"></property>
</bean> -->
<!-- 配置DAO类 -->
<!-- <bean id="accountDAO" class="com.demo1.AccountDAOImpl">
<property name="dataSource" ref="dataSource"/>
</bean> -->
</beans>
再把SpringDemo1.java改成这样
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDemo1 {
//测试业务层的类
// @Resource(name="accountService")
// private AccountService accountService;
//
// public void setAccountService(AccountService accountService) {
// this.accountService = accountService;
// }
@Test
public void demo1(){
//accountService.transfer("aaa", "bbb", 200d);
System.out.println("测试能打印输出吗?");
}
}
结果就是正确的运行测试
SpringDemo1.java文件和上面一样,然后把ApplicationContext.xml里的bean注释去掉,结果就报错