NoClassDefFoundError: org/apache/commons/pool/KeyedObjectPoolFactory BasicDataSource Spring - spring

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.

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,

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

Related

JNDI lookup failure

i would be grateful if someone can help me rectify the issue in my code. Not sure where I'm going wrong.
Currently my persistence.xml contains
<property name="hibernate.transaction.manager_lookup_class" value="#####.hibernate.JbossTSTransactionManagerLookup"/>
<property name="hibernate.current_session_context_class" value="jta"/>
along with
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="####"/>
<property name="jpaDialect" ref="jpaDialect"/>
</bean>
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManagerName" value="java:comp/env/TransactionManager"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
Context initialization failed : org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [###XMLConfig.xml]: Invocation of init method failed; nested exception is org.springframework.transaction.TransactionSystemException: JTA TransactionManager is not available at JNDI location [java:comp/env/TransactionManager]; nested exception is org.springframework.jndi.TypeMismatchNamingException: Object of type [class com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionManagerImple] available at JNDI location [java:comp/env/TransactionManager] is not assignable to [javax.transaction.TransactionManager]
Caused by: org.springframework.transaction.TransactionSystemException: JTA TransactionManager is not available at JNDI location [java:comp/env/TransactionManager]; nested exception is org.springframework.jndi.TypeMismatchNamingException: Object of type [class com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionManagerImple] available at JNDI location [java:comp/env/TransactionManager] is not assignable to [javax.transaction.TransactionManager]
at org.springframework.transaction.jta.JtaTransactionManager.lookupTransactionManager(JtaTransactionManager.java:598)
Caused by: org.springframework.jndi.TypeMismatchNamingException: Object of type [class com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionManagerImple] available at JNDI location [java:comp/env/TransactionManager] is not assignable to [javax.transaction.TransactionManager] at org.springframework.jndi.JndiTemplate.lookup()
at org.springframework.transaction.jta.JtaTransactionManager.lookupTransactionManager()
Looks like there is some issue with the classpath of your project. Check if there are two versions of the same dependency in your project. In my case I had multiple versions of the jboss-transaction jar in the classpath, keeping just one version of it fixed the issue for me. Check if there is any transitive dependency which is causing this issue which you can exclude in your maven pom.

Trying to deploy a Spring Service on WSO2 Application Service

I'm trying to deploy a Spring Service in WSO2 Application Service. I did THIS tutorial and the app works fine on Eclipse, but when I try to deploy it on WSO2 I get this error:
Cannot load Spring beans. Please check the Spring context
configuration file and verify that the defined Spring beans exist in
the .jar file.
I unziped de .jar file and JdbcCustomerDAO class is there with all the others.
Spring context:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="customerDAO" class="com.mkyong.customer.dao.impl.JdbcCustomerDAO">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="url" />
<property name="username" value="username" />
<property name="password" value="password" />
</bean>
</beans>
I also tried to include spring and mysql-connector-java jars on /repository/components/extensions as says HERE.
EDIT:
ERROR {org.wso2.carbon.springservices.ui.SpringServiceMaker} - Cannot
load Spring beans. Please check the Spring context configuration file
and verify that the defined Spring beans exist in the .jar file.
{org.wso2.carbon.springservices.ui.SpringServiceMaker}
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot
find class
[org.springframework.jdbc.datasource.DriverManagerDataSource] for bean
with name 'dataSource' defined in resource loaded through InputStream;
nested exception is java.lang.ClassNotFoundException:
org.springframework.jdbc.datasource.DriverManagerDataSource
Looks like it couldn't find spring-jdbc, so I added the jar to extensions but now I get this error:
ERROR {org.wso2.carbon.springservices.ui.SpringServiceMaker} - Cannot load
Spring beans. Please check the Spring context configuration file and
verify that the defined Spring beans exist in the .jar file.
{org.wso2.carbon.springservices.ui.SpringServiceMaker}
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'dataSource' defined in resource loaded
through InputStream: Instantiation of bean failed; nested exception is
org.springframework.beans.BeanInstantiationException: Could not
instantiate bean class
[org.springframework.jdbc.datasource.DriverManagerDataSource]:
Constructor threw exception; nested exception is
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
I understand that commons.logging is also missing, but when I try to add it to extensions, WSO2 doesn't start. I get a lot of
Log4j:ERROR Could not instantiate...

Spring context:property-placeholder can't resovle nested variables

I have two projects -- project-web, and project-service, both of them use Spring core 3.1.3 and have configurations to load properties from corresponding property files:
project-web -- Spring Integration based project, in its spring config file:
<context:property-placeholder location="WEB-INF/spring-integration/spring-integration.properties" ignore-resource-not-found="true" />
<import resource="classpath*:META-INF/spring/applicationContext.xml" />
where the import is to include the spring configuration file from project-service, and in the project-service project, I have following configured:
<context:property-placeholder location="classpath:META-INF/application.properties, classpath:META-INF/db.properties" ignore-resource-not-found="true"/>
<import resource="classpath:META-INF/spring/applicationContext-data.xml"/>
where the import to include Spring configuration for the DAOs, inside the applicationContext-data.xml I have:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${db.${db.type}.driver}" />
<property name="url" value="${db.${db.type}.url}"/>
<property name="username" value="${db.${db.type}.username}" />
<property name="password" value="${db.${db.type}.password}" />
</bean>
When I run the unit tests for project-service, everything is fine, all the variables are resolved correctly without any problem. But when I run the project-web (project-service will be included as a .jar file in the WEB-INF/lib folder of project-web), it throws error during start up saying can't resolve ${db.type}:
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'dataSource' defined in class path resource [META-INF/spring/applicationContext-data.xml]: Could not resolve placeholder 'db.type' in string value "db.${db.type}.driver"
at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:209) ~[spring-beans-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.processProperties(PropertySourcesPlaceholderConfigurer.java:174) ~[spring-context-3.1.3.RELEASE.jar:3.1.3.RELEASE]
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:151) ~[spring-context-3.1.3.RELEASE.jar:3.1.3.RELEASE]
......................
Note: I can't declare everything in the project-web, because project-service will be also used by other projects. Anyone know why in project-service works when it runs alone but not when included by the project-web? It can't resolve the nested variable ${db.type}
The problem is that your first PropertyPlaceHolderConfigurer is trying to resolve the placeholder that needs to be resolved by the second one.
You can either use a different prefix for each one (e.g. !{ instead of ${ for one of them), or set
ignore-unresolvable="true"
on the first one - then it will leave the resolution to the other one.

Class load error on Spring MVC project for Spring newbie

Warning: newbie alert!
I'm in early days of learning Spring and am trying to get my first app up and running which will simply read some data from a DB and display it.
I'm using SpringSource Tool Suite 2.8.0.RELEASE. I've created a new Spring MVC project and want to read some data from a local MySQL DB.
I wrote a simple DAO class:
package com.blah.blah;
import org.springframework.jdbc.core.support.JdbcDaoSuppo rt;
public class MyDAO extends JdbcDaoSupport {
I've added this to the pom.xml file:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
I've added this to the root-context.xml (is this the right config file to update?):
<bean id="myDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/dbname" />
<property name="username" value="root" />
<property name="password" value="mypw" />
</bean>
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate" >
<constructor-arg ref="myDataSource"></constructor-arg>
</bean>
<bean id="parentDAO"
class="org.springframework.jdbc.core.support.JdbcD aoSupport">
<property name="dataSource" ref="myDataSource"></property>
</bean>
When I right-click on the project and select Debug As > Debug On Server I get the error:
24-Mar-2012 16:13:42 org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of
class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.CannotLoadBeanClassException:
Cannot find class [org.springframework.jdbc.datasource.DriverManagerDataSource]
for bean with name 'myDataSource' defined in ServletContext resource
[/WEB-INF/spring/root-context.xml]; nested exception is
java.lang.ClassNotFoundException: org.springframework.jdbc.datasource.DriverManagerDataSource
I've been looking at this for a while and can't figure out what I'm doing wrong. I've found the folder where the app is deployed to (C:\Program Files\springsource\vfabric-tc-server-developer-2.6.1.RELEASE\spring-insight-instance\wtpwebapps\MyAppName\WEB-INF\lib on my machine) and the lib folder contains spring-jdbc-3.1.0.RELEASE.jar and when I open it, I can see the DriverManagerDataSource class file so I don't know why I'm getting the error above.
Any advice greatly appreciated.
Check that the Spring libraries are in the classpath so they are available for the server.
I had the same jar file included in the project twice. Removed one and it worked.
I had the same problem in Eclipse and creating a new workspace solved this problem.
I had added required jar source instead of release. Strange but changing that to release version fixed this problem.

Spring alias not available when running in Grails

I have a problem with Spring aliases in Grails. I have a library .jar file containing classes and Spring configuration that is not working as expected. It does work as expected when I import them from a standard (no Grails) Java app.
The current configuration contains this.
<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="contextPath" value="uk.co.company.package"/>
</bean>
<alias name="marshaller" alias="unmarshaller"/>
And fails with an error.
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'unmarshaller' is defined
Changing the configuration to the following then leads it to work as expected.
<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="contextPath" value="uk.co.company.package"/>
</bean>
<bean id="unmarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="contextPath" value="uk.co.company.package"/>
</bean>
The configuration is being imported and is being read. For some reason the alias is unavailable when I try to use it. What is that reason?
This is with Grails 1.3.7 and Spring 3.0.5.
I am seeing this issue as well. You can get around it by defining the alias in the Grails resources.xml or in my case in my plugin doWithSpring closure:
springConfig.addAlias "aliasName", "beanName"
I'd expect the importBeans to import alias as well
This link might be helpful for you:
http://burtbeckwith.com/blog/?p=85
It is mentioned there that aliases don't work at least when declared in the resources.xml. The post also mentions a way of declaring aliases programatically. But it seems like this post was written a while back and not sure how relevant it is with grails 1.3.7.

Resources