首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用@Cacheable注释时加载ApplicationContext失败

使用@Cacheable注释时加载ApplicationContext失败
EN

Stack Overflow用户
提问于 2018-01-24 16:06:27
回答 1查看 568关注 0票数 2

我正在将缓存集成到我的web应用程序中,但由于某种原因,应用程序上下文在添加@Cacheable注释时未能加载。

我已经试着解决这个问题两天了,你的帮助真的很感谢!

app.context.xml

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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>

正在缓存的方法。

代码语言:javascript
复制
public class SiteService implements ISiteService {

    @Cacheable("cacheSite")
        public JsonObject getSiteJson(String siteId, boolean istTranslated) { ... }

}

正在引发的异常

代码语言:javascript
复制
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'
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-26 10:35:04

@yegdom的评论实际上是正确的答案。当添加Cacheable注释时,Spring生成一个实现ISiteService的代理。在代码中的某个地方,您有一个需要SiteService的bean,即实现。

有三种解决方案(按优先顺序排列):

  • 删除无用的界面..。一个单一的实现只是增加了复杂性,而没有直接的好处。删除它将强制Spring使用类代理
  • 修复依赖关系以使用ISiteService
  • proxy-target-class="true"添加到cache:annotation-driven以告诉Spring创建类代理

我确实不推荐最后一个接口,因为您应该始终依赖于接口或始终依赖于类(并删除接口)。不是同时发生的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48426806

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档