spring注入groovy bean:找不到匹配的编辑器或转换策略

时间:2015-11-19 16:13:34

标签: java spring groovy

让我先谈谈我得到的例外的根本原因,

Caused by: java.lang.IllegalStateException: Cannot convert value of type [com.sun.proxy.$Proxy15 implementing com.company.product.statistics.StatisticsReporter,com.company.product.statistics.StatisticsAccumulator] to required type [com.company.product.statistics.StatisticsReporter] for property 'statisticsReporter': no matching editors or conversion strategy found
        at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:287) ~[spring-beans-4.1.0.RELEASE.jar:4.1.0.RELEASE]
        at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:440) ~[spring-beans-4.1.0.RELEASE.jar:4.1.0.RELEASE]
        ... 62 common frames omitted

这真的很奇怪,因为它基本上说它无法将实现StatisticsReporter的对象和其他东西转换为类型StatisticsReporter

当将一个groovy类注入Spring bean时发生了这种情况:

<lang:groovy id="clientsDetailsManager" script-source="classpath:com/company/product/security/CaseInsensitiveClientDetailsManager.groovy">
    <lang:property name="configuration" ref="confAuthentication"/>
    <lang:property name="statisticsReporter" ref="statisticsReporter"/>
</lang:groovy>

由于我对Java更加轻松,我尝试使用相同的bean注入但是使用了java类,并且它完全正常。但我需要让它在groovy中运作。任何有关问题的想法都将受到高度赞赏。

一些代码摘录(如果有意义的话,我只需告诉我添加其他内容):

java中的bean

<bean id="statisticsReporter" factory-bean="statisticsAdministrator" factory-method="getStatisticsReporter"/>

<bean id="clientsDetailsManager" class="com.company.product.security.CaseInsensitiveClientDetailsManager">
    <constructor-arg name="configuration" ref="confAuthentication"/>
    <constructor-arg name="statisticsReporter" ref="statisticsReporter"/>
</bean>

我在bean中注入的对象的Java类(仅构造函数):

public class CaseInsensitiveClientDetailsManager extends ClientDetailsManager {

    public CaseInsensitiveClientDetailsManager() {
        super();
    }

    public CaseInsensitiveClientDetailsManager(Client[] clients, StatisticsReporter statisticsReporter) {
        super(clients, statisticsReporter);
    }

    public CaseInsensitiveClientDetailsManager(Configuration configuration, StatisticsReporter statisticsReporter) {
        super(configuration, statisticsReporter);
    }    
    ...
}

父类的Java类(仅构造函数和一些变量):

public class ClientDetailsManager implements UserDetailsManager {

    protected ConcurrentMap<String, Client> cache;
    protected Configuration                 configuration;
    private StatisticsReporter              statisticsReporter;

    // empty constructor and setters for properties needed when groovy class as spring bean
    public ClientDetailsManager() {
        this.cache = new ConcurrentSkipListMap<>();     
    }

    public ClientDetailsManager(Configuration configuration, StatisticsReporter statisticsReporter) {
        this.cache = new ConcurrentSkipListMap<>();     
        this.configuration = configuration;
        this.statisticsReporter = statisticsReporter;
    }

    Configuration getConfiguration() {
        return configuration;
    }

    public void setConfiguration(Configuration configuration) {
        this.configuration = configuration;
    }

    public void setStatisticsReporter(StatisticsReporter statisticsReporter) {
        this.statisticsReporter = statisticsReporter;
    }

    /** Only for testing purposes. */
    public ClientDetailsManager(Client[] clients, StatisticsReporter statisticsReporter) {
        this.cache = new ConcurrentSkipListMap<>();
        ...
    }

    ...
}

它找不到的Java类&#34;转换策略&#34;注入groovy时:

public class StatisticsAdministrator extends AbstractAdministrator {
   …
    private StatisticsReporter    statisticsReporter;

    public StatisticsReporter getStatisticsReporter() {
        return statisticsReporter;
    }

}

对于我的试验,我使用了CaseInsensitiveClientDetailsManager的groovy版本,其内容完全是java类。

最后,堆栈跟踪的开始,虽然我不认为它是相关的:

  

2015-11-18 15:09:15,344 ERROR [main] Starter - 创建bean时出错   名字&#39; ocsHttpConnectorFactoryAdministrator&#39;在类路径中定义   resource [beans / nodes.ocs.xml]:无法解析对bean的引用   &#39;出口&#39;设置bean属性&#39; jmxBeanExporter&#39 ;;嵌套   异常是org.springframework.beans.factory.BeanCreationException:   创建名称为&#39; exporter&#39;的bean时出错在类路径中定义   resource [beans / jmx.xml]:无法解析对bean的引用   &#39; muticastClusterEngine&#39;同时设置bean属性&#39; broadcaster&#39 ;;   嵌套异常是   org.springframework.beans.factory.BeanCreationException:错误   创建名称为&#39; muticastClusterEngine&#39;在类路径中定义   resource [beans / cluster.xml]:无法解析对bean的引用   &#39; clientsDetailsManager&#39;设置构造函数参数时;嵌套   异常是org.springframework.beans.factory.BeanCreationException:   创建名称为&#39; clientsDetailsManager&#39;的bean时出错:   实例化bean之前的BeanPostProcessor失败;嵌套   异常是org.springframework.beans.factory.BeanCreationException:   创建名称为&#39; scriptedObject.clientsDetailsManager&#39;的bean时出错:   bean的初始化失败;嵌套异常是   org.springframework.beans.ConversionNotSupportedException:失败   转换类型&#39; com.sun.proxy。$ Proxy15实现的属性值   com.company.product.statistics.StatisticsReporter,com.company.product.statistics.StatisticsAccumulator&#39;   要求的类型&#39; com.company.product.statistics.StatisticsReporter&#39;   对于财产&statistics;统计报告&#39 ;;嵌套异常是   java.lang.IllegalStateException:无法转换类型的值   [com.sun.proxy。$ Proxy15实现   com.company.product.statistics.StatisticsReporter,com.company.product.statistics.StatisticsAccumulator]   到必需的类型[com.company.product.statistics.StatisticsReporter]   对于属性&quot; statisticsReporter&#39;:没有匹配的编辑器或转换   战略发现   org.springframework.beans.factory.BeanCreationException:错误   用名称&#39; ocsHttpConnectorFactoryAdministrator&#39;创建bean。定义   在类路径资源[beans / nodes.ocs.xml]中:无法解析引用   豆类出口商&#39;设置bean属性&#39; jmxBeanExporter&#39 ;;   嵌套异常是   org.springframework.beans.factory.BeanCreationException:错误   创建名为&#39; exporter&#39;的bean在类路径资源中定义   [beans / jmx.xml]:无法解析对bean的引用   &#39; muticastClusterEngine&#39;同时设置bean属性&#39; broadcaster&#39 ;;   嵌套异常是   org.springframework.beans.factory.BeanCreationException:错误   创建名称为&#39; muticastClusterEngine&#39;在类路径中定义   resource [beans / cluster.xml]:无法解析对bean的引用   &#39; clientsDetailsManager&#39;设置构造函数参数时;嵌套   异常是org.springframework.beans.factory.BeanCreationException:   创建名称为&#39; clientsDetailsManager&#39;的bean时出错:   实例化bean之前的BeanPostProcessor失败;嵌套   异常是org.springframework.beans.factory.BeanCreationException:   创建名称为&#39; scriptedObject.clientsDetailsManager&#39;的bean时出错:   bean的初始化失败;嵌套异常是   org.springframework.beans.ConversionNotSupportedException:失败   转换类型&#39; com.sun.proxy。$ Proxy15实现的属性值   com.company.product.statistics.StatisticsReporter,com.company.product.statistics.StatisticsAccumulator&#39;   要求的类型&#39; com.company.product.statistics.StatisticsReporter&#39;   对于财产&statistics;统计报告&#39 ;;嵌套异常是   java.lang.IllegalStateException:无法转换类型的值   [com.sun.proxy。$ Proxy15实现   com.company.product.statistics.StatisticsReporter,com.company.product.statistics.StatisticsAccumulator]   到必需的类型[com.company.product.statistics.StatisticsReporter]   对于属性&quot; statisticsReporter&#39;:没有匹配的编辑器或转换   战略发现...

     

引起:java.lang.IllegalStateException:无法转换值   输入[com.sun.proxy。$ Proxy15实现   com.company.product.statistics.StatisticsReporter,com.company.product.statistics.StatisticsAccumulator]   到必需的类型[com.company.product.statistics.StatisticsReporter]   对于属性&quot; statisticsReporter&#39;:没有匹配的编辑器或转换   战略发现           在org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:287)   〜[弹簧豆-4.1.0.RELEASE.jar:4.1.0.RELEASE]           在org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:440)   〜[弹簧豆-4.1.0.RELEASE.jar:4.1.0.RELEASE]           ...省略了62个常见帧

非常感谢!

0 个答案:

没有答案