【发布时间】:2011-10-28 10:34:53
【问题描述】:
我使用 Spring 3.0 和 ehcache。我在 bean 的某些方法中添加了 @Cacheable 注释。我将该 bean 注入到其他 bean 中,并在我的应用程序上下文 xml 文件中注册。该应用程序在添加 ehcache 注释之前工作(我使用 com.googlecode.ehcache-spring-annotations v 1.2.0),但在添加注释之后,Spring 无法正确注入包含注释的 bean。我在日志文件中看到的错误是:
org.springframework.beans.ConversionNotSupportedException:无法将“$Proxy67 实现 java.io.Serializable、org.springframework.aop.SpringProxy、org.springframework.aop.framework.Advised”类型的属性值转换为所需类型“ {my bean type}' 用于属性“{property}”。
这是我添加到我的应用程序上下文中以使 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 类型。
【问题讨论】:
-
有
@Cacheable注解的Bean是否有接口,你是使用该接口还是其他注入缓存的bean中的类? -
无论如何:如果我没记错的话,
@Cacheable支持是 Spring 3.1 的一个特性,3.0 不支持。确保您不使用javax.persistance.Cacheable! -
@Ralph,我确定我使用了正确的
@Cacheable注释。我刚刚与一些同事讨论了这个问题,您的第一条评论可能指向正确的答案。我正在使用具体类来注入 bean,而 Spring 似乎将代理绑定到接口。