尝试使用 spring jdbc 从 mysql 中提取数据时出错

An error while trying to pull data from mysql using spring jdbc

项目结构快照

进入 java 和 spring 仅 2 天。

尝试在 Maven 项目中使用 spring 从 mySQL 数据库中提取数据。

正在学习教程。

但是当代码为运行时,出现如下错误(仅附上错误的一部分)

下面附上所有代码。

错误:

Jul 25, 2015 11:00:15 PM   
org.springframework.context.support.AbstractApplicationContext prepareRefresh 
INFO: Refreshing    
org.springframework.context.support.ClassPathXmlApplicationContext@da2dbb:      startup date [Sat Jul 25 23:00:15 IST 2015]; root of context hierarchy
Jul 25, 2015 11:00:15 PM         
org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [com/caveofprogramming/spring/test/beans/Beans.xml]
Jul 25, 2015 11:00:16 PM    
org.springframework.core.io.support.PropertiesLoaderSupport loadProperties
INFO: Loading properties file from class path resource [com/caveofprogramming/spring/props/jdbc.properties]
Jul 25, 2015 11:00:16 PM  org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in    org.springframework.beans.factory.support.DefaultListableBeanFactory@f438e: defining beans [offersDao,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,dataSource]; root of factory hierarchy
Jul 25, 2015 11:00:16 PM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@f438e: defining beans [offersDao,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,dataSource]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'offersDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.caveofprogramming.spring.test.OffersDAO.setDataSource(javax.sql.DataSource); nested exception is java.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
at 

App.java

package com.caveofprogramming.spring.test;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {

public static void main(String[] args) {

    ApplicationContext context = new ClassPathXmlApplicationContext("com/caveofprogramming/spring/test/beans/Beans.xml");

    OffersDAO offersDao = (OffersDAO)context.getBean("offersDao");
    List<Offer> offers = offersDao.getOffers();

    for(Offer offer:offers){
        System.out.println(offer);
    }
    ((ClassPathXmlApplicationContext)context).close();
}

}

Offer.java

package com.caveofprogramming.spring.test;

public class Offer {
private String author;
private String title;

public String getAuthor() {
    return author;
}

public void setAuthor(String author) {
    this.author = author;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

@Override
public String toString() {
    return "Offer [author=" + author + ", title=" + title + "]";
}   

}

OffersDAO.java

package com.caveofprogramming.spring.test;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

@Component("offersDao")
public class OffersDAO {

    private JdbcTemplate jdbc;
    @Autowired
    public void setDataSource(DataSource jdbc) {
        System.out.println("TESTINGGGGGGGGG in setDataSource");
        this.jdbc = new JdbcTemplate(jdbc);
    }


    public List<Offer> getOffers(){

         return jdbc.query("select * from library.book", new RowMapper<Offer>(){

            public Offer mapRow(ResultSet rs, int rowNum) throws SQLException {

                Offer offer = new Offer();
                offer.setTitle(rs.getString("title"));
                offer.setAuthor(rs.getString("author"));
                return offer;
            }});


    }
}

jdbc.properties

jdbc.username = root
jdbc.password = 123456
jdbc.driver   = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/library

Beans.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:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.caveofprogramming.spring.test">    </context:component-scan>
    <context:property-placeholder
    location="com/caveofprogramming/spring/props/jdbc.properties" />


    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">

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

</beans>

如果需要更清楚的说明,请务必询问。 提前致谢

你的例外是 java.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool 这基本上意味着所需的 java class 不在 class 路径上。 问题是由于 commons-pool 不在您的 class 路径中。

  1. 理想情况下,当您将 apache tomcat 服务器添加为项目中的服务器时,这应该可用,并且这样做将从服务器

  2. 如果不是这种情况,请通过 downloading it

  3. 将其添加到 lib 文件夹中
  4. 如果它是一个 Maven 项目那么它在 pom.xml

    公共池 公共池 1.5.6