程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> spring-Spring使用JDBC過程報錯,求解答

spring-Spring使用JDBC過程報錯,求解答

編輯:編程綜合問答
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"/>                                   

最佳回答:


NoClassDefFoundError: org/apache/commons/pool2/PooledObjectFactory
dao層注入失敗了,看這信息像是少導入包了,類沒定義,導致注入失敗

如果是maven工程,加入下面的看看,如果不是,自己去找jar文件 ,然後添加進來

 <dependency>
    <groupId>commons-pool</groupId>
    <artifactId>commons-pool</artifactId>
    <version>1.6</version>
</dependency>
qq948216248
qq948216248
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved