水里的石头
2016-07-29 05:44
代码和老师的完全一样,视频看了好几遍了,测试的时候报错: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注释去掉,结果就报错
我也跟你有一样的问题,把test的代码改成下面这样的,就可以运行了,应该是注入有问题
@RunWith(BlockJUnit4ClassRunner.class) //@ImportResource("classpath:applicationContext1.xml") public class AccountTest { // @Resource(name = "accountService") // private AccountService accountService; @Test public void testDome(){ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext1.xml"); AccountService acc = (AccountService)context.getBean("accountService"); acc.transfer("aaa", "bbb", 200d); // accountService.transfer("aaa", "bbb", 200d); } }
请问一下Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'accountService' is defined这个问题是如何解决的,可以知道一下吗?谢谢
Class<BlockJUnit4ClassRunner> cannot be resolved to a type
看一下你的jdk编译版本,1.8不兼容,改成1.6就ok了,不是系统的,是项目的!右键项目>属性>compiler改成1.6。如果你开发用的1.8,这个时候项目会有小红叉,别管他。要想去掉小红叉,开发也用1.6。jdk1.8太多坑了……
请问你这个问题是怎么解决的,我也遇到了同样的问题。求助
你把异常的下面那部分,caused by哪里看一看,我怀疑你没有导入老师最后说的jbdc那个jar包。
AccountServiceImpl 你这个累里面的有这个名称的属性么?accountDAO
Spring事务管理
87318 学习 · 197 问题
相似问题