将org.apache.tomcat.jdbc.pool.DataSource转换为org.apache.commons.dbcp.BasicDataSource

时间:2015-05-11 19:21:53

标签: grails connection-pooling

从Grails 2.2.1升级到2.4.3其他所有内容都升级正常,但遇到Tomcat升级到7.0.54的问题我收到错误

Error initializing the application: Error creating bean with name
'dataSourceMBean': Unsatisfied dependency expressed through
constructor argument with index 0 of type
[org.apache.commons.dbcp.BasicDataSource]: Could not convert
constructor argument value of type
[org.apache.tomcat.jdbc.pool.DataSource] to required type
[org.apache.commons.dbcp.BasicDataSource]: Failed to convert value of
type 'org.apache.tomcat.jdbc.pool.DataSource' to required type
'org.apache.commons.dbcp.BasicDataSource'; nested exception is
java.lang.IllegalStateException: Cannot convert value of type
[org.apache.tomcat.jdbc.pool.DataSource] to required type
[org.apache.commons.dbcp.BasicDataSource]: no matching editors or
conversion strategy found
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'dataSourceMBean': Unsatisfied
dependency expressed through constructor argument with index 0 of type
[org.apache.commons.dbcp.BasicDataSource]: Could not convert
constructor argument value of type
[org.apache.tomcat.jdbc.pool.DataSource] to required type
[org.apache.commons.dbcp.BasicDataSource]: Failed to convert value of
type 'org.apache.tomcat.jdbc.pool.DataSource' to required type
'org.apache.commons.dbcp.BasicDataSource'; nested exception is
java.lang.IllegalStateException: Cannot convert value of type
[org.apache.tomcat.jdbc.pool.DataSource] to required type
[org.apache.commons.dbcp.BasicDataSource]: no matching editors or
conversion strategy found
     at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:722)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1114)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1017)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.codehaus.groovy.grails.commons.spring.DefaultRuntimeSpringConfiguration.getApplicationContext(DefaultRuntimeSpringConfiguration.java:156)
    at org.codehaus.groovy.grails.commons.spring.GrailsRuntimeConfigurator.initializeContext(GrailsRuntimeConfigurator.java:188)
    at org.codehaus.groovy.grails.commons.spring.GrailsRuntimeConfigurator.configure(GrailsRuntimeConfigurator.java:168)
    at org.codehaus.groovy.grails.commons.spring.GrailsRuntimeConfigurator.configure(GrailsRuntimeConfigurator.java:127)
    at org.codehaus.groovy.grails.web.context.GrailsConfigUtils.configureWebApplicationContext(GrailsConfigUtils.java:126)
    at org.codehaus.groovy.grails.web.context.GrailsContextLoaderListener.initWebApplicationContext(GrailsContextLoaderListener.java:109)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4973)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

我收到错误的区域已经存在了一段时间

dataSourceMBean(com.item.management.DataSourceMBean, ref('dataSourceUnproxied'))

课程是:

class DataSourceMBean {     
   private BasicDataSource dataSource       
   public DataSourceMBean(BasicDataSource dataSource) {         
      this.dataSource = dataSource  
   }

当我将dataSource更改为特定类型时:

private org.apache.tomcat.jdbc.pool.DataSource dataSource

一切都在编译,所有测试都运行良好 问这个什么时候改变了? 是否有替代我应该使用的BasicDataSource?

我找到了这篇文章,但不确定是否适用 java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be cast to org.apache.tomcat.jdbc.pool.DataSource

1 个答案:

答案 0 :(得分:1)

除非你真的需要它是BasicDataSource(而且我不明白你为什么会这样做),否则不要将它改为更具体的类,将其更改为界面,{ {1}}。如果确实需要特定的实现类,可以通过在javax.sql.DataSource中注册dataSource并设置所需的属性来定义自己的org.apache.commons.dbcp.BasicDataSource bean,例如。

resources.groovy

只要您使用相同的bean名称,就可以使用Grails注册的任何bean或插件执行此操作。