7

I'm new to Spring, still learning. I'm using Spring Tool Suite version 3.5 with Java 6 on my Mac. I'm attempting to use BasicDataSource

<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

I have the following jar files on my class path: commons-dbcp-1.4.jar, commons-pool2-2.2.jar, commons-collections4-4.0.jar. But I'm still seeing a NoClassDefFoundError reference to KeyedObjectPoolFactory.

Error creating bean with name 'dataSource' defined in class path resource [test-   infrastructure-config.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/pool/KeyedObjectPoolFactory
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:630)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:148)

I've searched for an answer and found an existing similar question, but unlike that one, I've got the JARs on my classpath.

I have trouble formatting code in this forum. My XML code isn't appearing. Sorry.

Community
  • 1
  • 1
user3636521
  • 71
  • 1
  • 1
  • 2
  • 1
    Might be a problem with the version number of the jars .... just check whether the version numbers you used for the jars are compatible with one another – Raju Rudru May 14 '14 at 12:21
  • Hi Raju Rudru, Yes that I've discovered that this is the problem. – user3636521 May 14 '14 at 12:31

2 Answers2

8

You're mixing up the versions. The KeyedObjectPoolFactory class exists in the 1.x branch of commons-pool, but not in 2.x. You should try with commons-pool-1.5.4 instead (which is the correct version dependency for commons-dbcp-1.4)

And may I suggest using eg. Maven to manage your dependencies - you'll get the transitive dependencies versioned correctly for free (mostly at least...)

Cheers,

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
  • 1
    It looks like I need to wait 8 hours before answering my own question because I have less than 10 reputation? But thanks to Anders R.Bystrup and Raju Rudru, Yes In the meantime I've discovered that you are both right. Here's the Javadoc for Commons Pool 2.2 API http://commons.apache.org/proper/commons-pool/api-2.2/index.html And here is version 1.6 http://commons.apache.org/proper/commons-pool/api-1.6/index.html The version 1.6 is the last version that contains KeyedObjectPoolFactory. So I've obtained commons-pool-1.6.jar and added it to my classpath and resolved the error. – user3636521 May 14 '14 at 12:34
  • I can't vote up either. But thank you both anyway :) – user3636521 May 14 '14 at 12:35
0

you could import the .Class by youself. import org.apache.commons.pool.KeyedObjectPoolFactory;