我正在将缓存集成到我的web应用程序中,但由于某种原因,应用程序上下文在添加@Cacheable注释时未能加载。
我已经试着解决这个问题两天了,你的帮助真的很感谢!
app.context.xml
<cache:annotation-driven cache-manager="EhCacheManagerBean" key-generator="customKeyGenerator" />
<bean id="EhCacheManagerBean" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcacheBean" />
<bean id="ehcacheBean" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="classpath:EhCache.xml" p:shared="true" />
<bean id ="customKeyGenerator" class="com.app.site.v2.cache.customKeyGenerator"/>
<bean id="siteService" class="com.app.site.v2.SiteService" primary="true"/>EhCache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="true"
monitoring="autodetect"
dynamicConfig="true">
<diskStore path="java.io.tmpdir" />
<cache name="cacheSite"
maxEntriesLocalHeap="100"
maxEntriesLocalDisk="1000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU"
transactionalMode="off">
<persistence strategy="localTempSwap" />
</cache>
正在缓存的方法。
public class SiteService implements ISiteService {
@Cacheable("cacheSite")
public JsonObject getSiteJson(String siteId, boolean istTranslated) { ... }
}正在引发的异常
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'siteService' is expected to be of type 'com.app.site.v2.SiteService' but was actually of type 'com.sun.proxy.$Proxy57'发布于 2018-01-26 10:35:04
@yegdom的评论实际上是正确的答案。当添加Cacheable注释时,Spring生成一个实现ISiteService的代理。在代码中的某个地方,您有一个需要SiteService的bean,即实现。
有三种解决方案(按优先顺序排列):
ISiteServiceproxy-target-class="true"添加到cache:annotation-driven以告诉Spring创建类代理我确实不推荐最后一个接口,因为您应该始终依赖于接口或始终依赖于类(并删除接口)。不是同时发生的。
https://stackoverflow.com/questions/48426806
复制相似问题