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

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
    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>
    
    点赞 评论
  • Royal_lr
    Royal_lr 2015-11-08 03:41

    又是你,,,还是那个错误
    @Resource(name="dataSource")

    public void setJdbcTemplate(DataSource dataSource) {

    this.jdbcTemplate = new JdbcTemplate(dataSource);

    }

    ,,不能这样注入的,,我已经回答过一次了

    点赞 评论

相关推荐