stackh34p
stackh34p

Reputation: 9009

Bean that uses ehcache cannot be injected

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.

Upvotes: 1

Views: 2232

Answers (1)

Ralph
Ralph

Reputation: 120811

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.

Upvotes: 2

Related Questions