1
votes

I use Spring 3.0 and ehcache. I have added @Cacheable annotations to some methods of a bean. I am injecting that bean into other beans and it is registered in my application context xml file. The application was working before adding ehcache annotations (I use com.googlecode.ehcache-spring-annotations v 1.2.0), but after adding the annotations, Spring is unable to properly inject the beans that contain the annotations. The error I see in my log file is:

org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type '$Proxy67 implementing java.io.Serializable,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised' to required type '{my bean type}' for property '{property}'.

Here is what I have added to my application context for ehcache to work:

<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>

I think the configuration is OK, because at first I had some issues loading the ehcache.xml file and there was a corresponding error in the logs for that. After I fixed the problem, I got the error above. It seems that spring creates a proxy for my bean that internally calls the caching logic for ehcache, but fails to make that proxy convertible to the bean type.

1
Does the Bean that has the @Cacheable Annotation have a interface, and do you use the interface or the class in the other bean where the cached one is injected? - Ralph
Anyway: If I remember right, then the @Cacheable support is a Spring 3.1 feature, not supported in 3.0. Make sure that you do not use javax.persistance.Cacheable ! - Ralph
@Ralph, I am certain I use the right @Cacheable annotation. I just discussed the issue with some colleagues and your first comment probably points to the right answer. I am using the concrete class to inject the bean, while Spring seems to bind the proxy to an interface. - Ivaylo Slavov

1 Answers

2
votes

See Spring (3.1) Reference: Chapter 27. Cache Abstraction

In your configuration, you are using, Interface Base Proxies. So the Bean that uses the Bean with the cached Method must refer to its interface, not to its concrete class.

Or you can change the configuration proxy-target-class="true" to use class based proxies.