我有带有hazelcast集成的spring-boot应用程序。
现在,我还需要添加ehcache支持。 @EnableCaching创建Hazelcast CacheManager。我的目标是将ehcache 3与@Cachable一起使用。

我尝试手动定义Ehcache CacheManager

  @Bean
  public CacheManager ehcache() {
    CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder().build();
    cacheManager.init();
    return cacheManager;
  }


但我有:

org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'ehcache' is expected to be of type 'org.springframework.cache.CacheManager' but was actually of type 'org.ehcache.core.EhcacheManager'.

最佳答案

对于Ehcache 3,您必须使用JCacheCacheManager包装Ehcache CacheManager并将其转换为Spring CacheManager。一个示例可用here

@Bean
public CacheManager cacheManager() {
    CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder().build(true);
    JCacheCacheManager jCacheCacheManager = new JCacheCacheManager(cacheManager);
    return jCacheCacheManager;
 }

关于java - 如何强制Spring(Boot)使用ehcache 3,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48886351/

10-10 15:10