用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>
? ? ? ? <!-- 配置業(yè)務層的類 -->
<!-- ? ? ? ? <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 {
//測試業(yè)務層的類
// @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注釋去掉,結果就報錯
2016-08-07
我也跟你有一樣的問題,把test的代碼改成下面這樣的,就可以運行了,應該是注入有問題
2017-10-28
請問一下Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'accountService' is defined這個問題是如何解決的,可以知道一下嗎?謝謝
2017-08-07
Class<BlockJUnit4ClassRunner> cannot be resolved to a type
2016-08-25
看一下你的jdk編譯版本,1.8不兼容,改成1.6就ok了,不是系統(tǒng)的,是項目的!右鍵項目>屬性>compiler改成1.6。如果你開發(fā)用的1.8,這個時候項目會有小紅叉,別管他。要想去掉小紅叉,開發(fā)也用1.6。jdk1.8太多坑了……
2016-08-24
請問你這個問題是怎么解決的,我也遇到了同樣的問題。求助
2016-08-02
你把異常的下面那部分,caused by哪里看一看,我懷疑你沒有導入老師最后說的jbdc那個jar包。
2016-07-29
AccountServiceImpl 你這個累里面的有這個名稱的屬性么?accountDAO?