我使用Spring 3.0和ehcache。我已将 @Cacheable
注释添加到 bean 的某些方法中。我将该 bean 注入(inject)到其他 bean 中,并将其注册在我的应用程序上下文 xml 文件中。应用程序在添加 ehcache 注释之前可以正常工作(我使用 com.googlecode.ehcache-spring-annotations v 1.2.0),但是在添加注释之后,Spring 无法正确注入(inject)包含注释的 bean。我在日志文件中看到的错误是:
org.springframework.beans.ConversionNotSupportedException:无法将“$Proxy67 实现 java.io.Serialized、org.springframework.aop.SpringProxy、org.springframework.aop.framework.Advised”类型的属性值转换为所需类型“属性“{property}”的“{my bean type}”。
以下是我添加到应用程序上下文中以使 ehcache 正常工作的内容:
<context:annotation-config />
<context:component-scan base-package="{my root package}" />
<ehcache:annotation-driven cache-manager="cacheManager" />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml" />
</bean>
我认为配置没问题,因为一开始我在加载 ehcache.xml 文件时遇到了一些问题,并且日志中存在相应的错误。解决问题后,我得到了上面的错误。看来 spring 为我的 bean 创建了一个代理,该代理在内部调用 ehcache 的缓存逻辑,但无法使该代理可转换为 bean 类型。
最佳答案
参见 Spring (3.1) 引用:章节 27. Cache Abstraction
在您的配置中,您正在使用接口(interface)基础代理。 因此,使用具有缓存方法的 Bean 的 Bean 必须引用其接口(interface),而不是其具体类。
或者您可以更改配置proxy-target-class="true"
以使用基于类的代理。
关于java - 使用ehcache的Bean无法注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7928111/