org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao': Injection of resource dependencies failed; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/pool2/PooledObjectFactory
userDao代碼如下:
package com.xiaoming.spring.jdbc.dao;
import javax.annotation.Resource;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.xiaoming.spring.jdbc.model.User;
@Repository("userDao")
public class UserDao implements IUserDao{
private JdbcTemplate jdbcTemplate;
@Resource(name="dataSource")
public void setJdbcTemplate(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
@Override
public void insert(User user) {
jdbcTemplate.update("insert into tab_user(username,password) values (?,?)",
user.getUsername(),user.getPassword());
System.out.println("插入"+user);
}
@Override
public void query(int id) {
// TODO Auto-generated method stub
}
@Override
public void del(int id) {
// TODO Auto-generated method stub
}
@Override
public void update(User user) {
// TODO Auto-generated method stub
}
beans文件如下:
<?xml version="1.0" encoding="UTF-8"?>
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.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:annotation-config> </context:annotation-config>
<context:component-scan base-package="com.xiaoming.spring.jdbc"></context:component-scan>
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="$(jdbc.driverClassName)"></property>
<property name="url" value="$(jdbc.url)"></property>
<property name="username" value="$(jdbc.username)"></property>
<property name="password" value="$(jdbc.password)"></property>
</bean>
<context:property-placeholder location="jdbc.properties"/>
NoClassDefFoundError: org/apache/commons/pool2/PooledObjectFactory
dao層注入失敗了,看這信息像是少導入包了,類沒定義,導致注入失敗
如果是maven工程,加入下面的看看,如果不是,自己去找jar文件 ,然後添加進來
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>