【问题标题】:unable to create bean of org.apache.commons.dbcp.BasicDataSource in spring-servlet.xml file无法在 spring-servlet.xml 文件中创建 org.apache.commons.dbcp.BasicDataSource 的 bean
【发布时间】:2017-11-17 22:45:45
【问题描述】:

我正在创建一个带有休眠集成的 spring-mvc 项目。我添加了 commons-dbcp 1.4 jar 文件,其中包含 BasicDataSource.class,但当我尝试在我的 spring-servlet.xml 文件中创建它的 bean 时,它仍然给我以下错误:

例外是 java.lang.ClassNotFoundException:

org.apache.commons.dbcp.BasicDataSource

我的 spring-servlet.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:tx="http://www.springframework.org/schema/tx" 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.0.xsd
                      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="work"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"></property>
<property name="order" value="3"></property>
</bean>

<bean id="ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost/spring"></property>
<property name="username" value="root"></property>
<property name="password" value=""></property>
</bean>
<bean id="bean" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="ds"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true></prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory></prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>shops.hbm.xml</value>
<value>users.hbm.xml</value>
<value>vehicle.hbm.xml</value>
</list>
</property>
</bean>
<bean id="dao" class="work.OperationDao">
</bean>
</beans>

【问题讨论】:

  • 你把jar添加到你的项目类路径了吗??
  • 是的,我在项目的类路径中添加了 jar。

标签: java spring hibernate spring-mvc servlets


【解决方案1】:

请将以下依赖项添加到 pom.xml

<dependency>
    <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
    <version>1.4</version>
</dependency>

【讨论】:

    猜你喜欢
    • 2013-04-11
    • 2016-02-20
    • 1970-01-01
    • 2021-07-12
    • 2018-03-20
    • 1970-01-01
    • 2017-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多