21

I was trying to do some basic database operations. I'm using STS 3.2.0, Apache Tomcat 7.

My dispatcher-servlet.xml includes:

<bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/mydb" />
        <property name="username" value="root" />
        <property name="password" value="root" />
    </bean>

Apache Tomcat fails miserably with thousands of exceptions, starting with this one :

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.jdbc.datasource.DriverManagerDataSource] for bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.jdbc.datasource.DriverManagerDataSource
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.jdbc.datasource.DriverManagerDataSource] for bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.jdbc.datasource.DriverManagerDataSource

And these are my library definitions

EDIT 1: I also downloaded 3.0.3.RELEASE version of the jar, and its still the same.

FINAL EDIT It seems, you should add your external jar files manually to your deployment directory as well (well if you don't use any extension that'd do that for you)

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
JuniorDev
  • 1,170
  • 2
  • 9
  • 19
  • Is that class actually in the jar file (open it with winzip or expand it in eclipse). If not then you have the wrong jar. If it is there then you are not picking it up when you start your app. – Dave Richardson May 10 '13 at 09:03
  • @DaveRlz I think it is in the jar. I can import and directly use its contents on my regular java files, it just does not seem to exist while my webapp is being started. – JuniorDev May 10 '13 at 10:10

3 Answers3

50

Try to add to POM.xml:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>3.0.3.RELEASE</version>
</dependency>
Patison
  • 2,591
  • 1
  • 20
  • 33
  • I've regenerated my pom.xml file and added jdbc dependency, but result is same. – JuniorDev May 10 '13 at 10:53
  • 1
    does your war file contain that jar in WEB-INF/lib folder? – Patison May 10 '13 at 11:33
  • 1
    well at least now I understand why I should use extensions like maven. The problem was, even though I added those jar to my project, they simply didn't exist in my WEB-INF/lib folder. I'll add these result to my original post, and select yours as an answer. Thanks! – JuniorDev May 10 '13 at 11:42
  • check https://mvnrepository.com/artifact/org.springframework/spring-jdbc for latest version – prayagupa Apr 12 '20 at 21:29
0

I think you should import selected jdbc jar into web app library.

Enrico
  • 19
  • 2
0

if none of the above solution doesn't work, then manually add spring-jdbc jar file to webapp/WEB-INF/lib.

ashish pandey
  • 1,653
  • 1
  • 10
  • 15