22

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)

2
  • 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. Commented May 10, 2013 at 9: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. Commented May 10, 2013 at 10:10

3 Answers 3

53

Try to add to POM.xml:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>3.0.3.RELEASE</version>
</dependency>
Sign up to request clarification or add additional context in comments.

4 Comments

I've regenerated my pom.xml file and added jdbc dependency, but result is same.
does your war file contain that jar in WEB-INF/lib folder?
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!
0

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

Comments

0

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

2 Comments

Try to expand on your answer a bit
download spring-jdbc-5.0.2.RELEASE.jar and copy it, in webapp/WEB-INF/lib. this jar version may vary for your project. so download jar version which is suitable for your project

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.