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

Convert org.apache.tomcat.jdbc.pool.DataSource to org.apache.commons.dbcp.BasicDataSource

从 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.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是:

class DataSourceMBean {     
   private BasicDataSource dataSource       
   public DataSourceMBean(BasicDataSource dataSource) {         
      this.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

除非你真的需要它是一个BasicDataSource(我不明白你为什么会这样),不要把它改成更具体的class,改成界面, javax.sql.DataSource.如果您确实需要特定的实现 class,您可以通过在 resources.groovy 中注册 org.apache.commons.dbcp.BasicDataSource 并设置所需的任何属性来定义自己的 dataSource bean,例如

import org.apache.commons.dbcp.BasicDataSource

beans = {
   dataSourceUnproxied(BasicDataSource) {
      driverClassName = 'foo'
      username = 'bar'
      password = 'secret'
      url = 'the_url'
      ...
   }
   ...
}

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