1. 程式人生 > >菜鳥學習Spring框架day_01天記錄NoSuchBeanDefinitionException

菜鳥學習Spring框架day_01天記錄NoSuchBeanDefinitionException

菜鳥學習Spring框架day_01天記錄

NoSuchBeanDefinitionException

錯誤一:
Error creating bean with name ‘dataSource’ defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean ‘oracle.jdbc.driver.OracleDriver’ while setting bean property ‘driverClassName’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException

: No bean named ‘oracle.jdbc.driver.OracleDriver’ is defined

剛學習一般 都是程式碼寫錯了,根據錯誤提示中的 [applicationContext.xml] 和 ‘dataSource’ 找到applicationContext.xml中配置資料來源的程式碼

<context:property-placeholder location="classpath:config/db.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">


<property name="driverClassName" ref="${db.driver}"></property>
<property name="url" ref="${db.url}"></property>
<property name="username" ref="${db.username}"></property>
<property name="password" ref="${db.password}"></property>
</bean>

發現ref應該全部改成value

錯誤二:
Error creating bean with name ‘SqlSessionFactory’ defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean ‘config/config.xml’ while setting bean property ‘configLocation’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ‘config/config.xml’ is defined
跟上一個錯誤一樣的

<bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" ref="config/config.xml"></property>
</bean>

沒有分清楚ref和value

value:設定普通資料
ref:引用資料,一般是另一個bean id值

正確的applicationContext.xml檔案

<?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:p="http://www.springframework.org/schema/p " xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 引入DB檔案 -->
<context:property-placeholder location="classpath:config/db.properties"/>
<!-- 配置資料來源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${db.driver}"></property>
<property name="url" value="${db.url}"></property>
<property name="username" value="${db.username}"></property>
<property name="password" value="${db.password}"></property>
</bean>
<!-- 配置SessionFactory -->
<bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 把資料來源注入到sessionFactory中 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 載入config核心配置檔案 -->
<property name="configLocation" value="config/config.xml"></property>
</bean>
<!-- 動態例項化介面 -->
<bean id="personMapper" class="com.lhm.dao.impl.PersonDaoImpl">
<!-- 注入sessionFactory -->
<property name="sqlSessionFactory" ref="SqlSessionFactory"></property>
</bean>
```