qq948216248 2015-11-07 10:42 采纳率: 33.3%
浏览 2107
已采纳

Spring使用JDBC过程报错,求解答

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"/>                                   
  • 写回答

2条回答 默认 最新

  • Evankaka 博客专家认证 2015-11-07 11:22
    关注

    NoClassDefFoundError: org/apache/commons/pool2/PooledObjectFactory
    dao层注入失败了,看这信息像是少导入包了,类没定义,导致注入失败

    如果是maven工程,加入下面的看看,如果不是,自己去找jar文件 ,然后添加进来

     <dependency>
        <groupId>commons-pool</groupId>
        <artifactId>commons-pool</artifactId>
        <version>1.6</version>
    </dependency>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#网络#的问题:网络是从楼上引一根网线下来,接了2台傻瓜交换机,也更换了ip还是不行
  • ¥15 资源泄露软件闪退怎么解决?
  • ¥15 CCF-CSP 2023 第三题 解压缩(50%)
  • ¥30 comfyui openpose报错
  • ¥20 Wpf Datarid单元格闪烁效果的实现
  • ¥15 图像分割、图像边缘提取
  • ¥15 sqlserver执行存储过程报错
  • ¥100 nuxt、uniapp、ruoyi-vue 相关发布问题
  • ¥15 浮窗和全屏应用同时存在,全屏应用输入法无法弹出
  • ¥100 matlab2009 32位一直初始化