为了账号安全,请及时绑定邮箱和手机立即绑定

用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注释去掉,结果就报错

正在回答

7 回答

我也跟你有一样的问题,把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);
	}

}


0 回复 有任何疑惑可以回复我~
#1

水里的石头 提问者

非常感谢!
2016-08-08 回复 有任何疑惑可以回复我~

请问一下Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'accountService' is defined这个问题是如何解决的,可以知道一下吗?谢谢


0 回复 有任何疑惑可以回复我~

Class<BlockJUnit4ClassRunner> cannot be resolved to a type

0 回复 有任何疑惑可以回复我~

看一下你的jdk编译版本,1.8不兼容,改成1.6就ok了,不是系统的,是项目的!右键项目>属性>compiler改成1.6。如果你开发用的1.8,这个时候项目会有小红叉,别管他。要想去掉小红叉,开发也用1.6。jdk1.8太多坑了……

0 回复 有任何疑惑可以回复我~
#1

水里的石头 提问者

谢谢,以后多交流
2016-08-26 回复 有任何疑惑可以回复我~

请问你这个问题是怎么解决的,我也遇到了同样的问题。求助

0 回复 有任何疑惑可以回复我~
#1

水里的石头 提问者

目前还没有彻底解决,咱一块学吧
2016-08-24 回复 有任何疑惑可以回复我~
#2

吼哈 回复 水里的石头 提问者

看一下你的jdk编译版本,1.8不兼容,改成1.6就ok了,不是系统的,是项目的!右键项目>属性>compiler改成1.6。如果你开发用的1.8,这个时候项目会有小红叉,别管他。要想去掉小红叉,开发也用1.6。jdk1.8太多坑了……
2016-08-25 回复 有任何疑惑可以回复我~
#3

Heather1 回复 水里的石头 提问者

我按照上面“风鹤子”的那种不使用注解的方法即可运行成功。
2016-08-25 回复 有任何疑惑可以回复我~
#4

吼哈 回复 Heather1

当然了,这里的不兼容是junit4的不兼容,你不用注解就相当于spring中动态的加载配置文件,肯定是没问题的啊。
2016-08-26 回复 有任何疑惑可以回复我~
查看1条回复

AccountServiceImpl 你这个累里面的有这个名称的属性么?accountDAO 

0 回复 有任何疑惑可以回复我~
#1

水里的石头 提问者

谢谢你的回答,我的AccountServiceImpl.java里面有accountDAO 代码如下, private AccountDAO accountDAO; public void setAccountDAO(AccountDAO accountDAO) { this.accountDAO = accountDAO; } 期待高手帮我找出原因,多谢
2016-07-29 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

用eclipse照视频抄写老师的代码,测试时侯无法加载bean文件,求解!

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信