Spring容器无法获取DBCP数据源

时间:2012-09-17 20:59:04

标签: java-ee spring-mvc datasource apache-commons-dbcp

背景

我是春天的新手。我正在使用Spring MVC 3和Springsource的spring工具套件。我正在运行他们提供的示例Spring模板。我正在使用Apache DBCP作为我的数据源。

问题

我已经能够使用注释注入bean但是我无法获取容器来获取我在servlet-context.xml文件中定义的数据源。内容如下。如果我自动装配我的数据源,那么在任何地方使用它都会得到一个空指针异常,表明没有注入依赖。这适用于我尝试自动装配的任何其他类。我很确定它与我定义bean的方式有关 在XML文件中,但我已经看到了几种不同的方法,所以我不确定什么是适合我的版本。

<?xml version="1.0" encoding="UTF-8"?>

http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd“&gt;

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <beans:property name="driverClassName" value="org.mysql.jdbc"/>
    <beans:property name="url" value="jdbc:mysql://localhost:3306/mydb"/>
    <beans:property name="username" value="root"/>
    <beans:property name="password" value="admin"/>
</beans:bean>   

<context:component-scan base-package="com.mycompany.myapp2" />

服务器返回的部分错误是

  

java.lang.NoClassDefFoundError:org / apache / commons / pool / KeyedObjectPoolFactory

1 个答案:

答案 0 :(得分:0)

KeyedObjectPoolFactory是commons-pool工件的一部分

在你的pom上添加它。

<dependency>
    <groupId>commons-pool</groupId>
    <artifactId>commons-pool</artifactId>
    <version>1.6</version>
</dependency>