当前位置: 首页 > 知识库问答 >
问题:

在没有XML的Spring 4中使用EhCache

陈誉
2023-03-14

在Spring 4或Spring Boot中,有没有一种不用xml初始化EhCache的方法?

我注意到Spring Boot1.0.0.RC3没有任何ehcache依赖项,但是Spring 4.0GA发布帖子提到它改进了对EhCache的支持。此外,Spring 3有类org.springframework.cache.ehcache.EhCacheCacheManager,但这不再是依赖项的一部分。

编辑:Spring 4确实有EhCache支持。您必须添加依赖项:

<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>

Edit2:我试过以下方法,我想我很接近,但我遇到了一个错误:

@Bean
@Override
public CacheManager cacheManager() {
    CacheConfiguration cacheConfiguration = new CacheConfiguration();
    cacheConfiguration.setName("primary");
    cacheConfiguration.setMemoryStoreEvictionPolicy("LRU");
    cacheConfiguration.setMaxEntriesLocalHeap(0);

    net.sf.ehcache.config.Configuration config = new net.sf.ehcache.config.Configuration();
    config.addCache(cacheConfiguration);

    net.sf.ehcache.CacheManager cacheManager = new net.sf.ehcache.CacheManager(config);
    cacheManager.setName("EhCache");

    return new EhCacheCacheManager(cacheManager);
}

@Bean
public EhCacheManagerFactoryBean factoryBean() {
    return new EhCacheManagerFactoryBean();
}

错误

Caused by: net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following:
1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary
2. Shutdown the earlier cacheManager before creating new one with same name.
The source of the existing CacheManager is: [Programmatically configured]
    at net.sf.ehcache.CacheManager.assertNoCacheManagerExistsWithSameName(CacheManager.java:590)
    at net.sf.ehcache.CacheManager.init(CacheManager.java:384)
    at net.sf.ehcache.CacheManager.<init>(CacheManager.java:263)
    at org.springframework.cache.ehcache.EhCacheManagerFactoryBean.afterPropertiesSet(EhCacheManagerFactoryBean.java:166)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
    ... 15 more

共有2个答案

胥玮
2023-03-14

我在两个抽象级别上执行此操作,每个技术的配置文件(Ehcache、Redis等)和一般配置文件。

下面是Ehcache的例子(Redis也类似):

@Configuration
public class EhCacheConfiguration {

    @Bean
    public EhCacheCacheManager ehCacheCacheManager() {

        return new EhCacheCacheManager(ehCacheManagerFactoryBean().getObject());
    }


    @Bean
    public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() {

        EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean();

        cacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
        cacheManagerFactoryBean.setShared(true);

        return cacheManagerFactoryBean;
    }
}

这是通用的(完成Redis钩子):

@Configuration
@EnableCaching
public class CachingConfiguration implements CachingConfigurer {

    @Qualifier("ehCacheCacheManager")
    @Autowired(required = false)
    private CacheManager ehCacheCacheManager;

    @Qualifier("redisCacheManager")
    @Autowired(required = false)
    private CacheManager redisCacheManager;


    @Bean
    @Override
    public CacheManager cacheManager() {

        List<CacheManager> cacheManagers = Lists.newArrayList();

        if (this.ehCacheCacheManager != null) {
            cacheManagers.add(this.ehCacheCacheManager);
        }

        if (this.redisCacheManager != null) {
            cacheManagers.add(this.redisCacheManager);
        }

        CompositeCacheManager cacheManager = new CompositeCacheManager();

        cacheManager.setCacheManagers(cacheManagers);
        cacheManager.setFallbackToNoOpCache(false);

        return cacheManager;
    }


    @Bean
    @Override
    public KeyGenerator keyGenerator() {

        return new DefaultKeyGenerator();
    }
}
师向文
2023-03-14

Spring中EhCache的无XML配置

@Configuration
@EnableCaching
public class CachingConfig implements CachingConfigurer {
    @Bean(destroyMethod="shutdown")
    public net.sf.ehcache.CacheManager ehCacheManager() {
        CacheConfiguration cacheConfiguration = new CacheConfiguration();
        cacheConfiguration.setName("myCacheName");
        cacheConfiguration.setMemoryStoreEvictionPolicy("LRU");
        cacheConfiguration.setMaxEntriesLocalHeap(1000);

        net.sf.ehcache.config.Configuration config = new net.sf.ehcache.config.Configuration();
        config.addCache(cacheConfiguration);

        return net.sf.ehcache.CacheManager.newInstance(config);
    }

    @Bean
    @Override
    public CacheManager cacheManager() {
        return new EhCacheCacheManager(ehCacheManager());
    }

    @Bean
    @Override
    public KeyGenerator keyGenerator() {
        return new SimpleKeyGenerator();
    }

    @Bean
    @Override
    public CacheResolver cacheResolver()    {
        return new SimpleCacheResolver();
    }

    @Bean
    @Override
    public CacheErrorHandler errorHandler() {
         return new SimpleCacheErrorHandler();
    }
}

或者,对于测试,您可以使用一个简单的ConcurrentMapCache运行,不使用XML,如下所示。

@Configuration
@EnableCaching
public class CachingConfig implements CachingConfigurer {
    @Bean
    @Override
    public CacheManager cacheManager() {
        SimpleCacheManager cacheManager = new SimpleCacheManager();

        List<Cache> caches = new ArrayList<Cache>();
        caches.add(new ConcurrentMapCache("myCacheName"));
        cacheManager.setCaches(caches);

        return cacheManager;
    }

    @Bean
    @Override
    public KeyGenerator keyGenerator() {
        return new SimpleKeyGenerator();
    }

    @Bean
    @Override
    public CacheResolver cacheResolver()    {
        return new SimpleCacheResolver();
    }

    @Bean
    @Override
    public CacheErrorHandler errorHandler() {
         return new SimpleCacheErrorHandler();
    }
}

编辑:更新以在基础缓存上添加关闭方法

编辑:添加了错误处理程序和缓存解析器的配置

编辑:更改SimpleCacheResolver上的构造函数调用,解决CacheManager不得为空问题(Spring v5.1)

@Configuration
@EnableCaching
public class CachingConfig implements CachingConfigurer {

    public static final String USER_CACHE_INSTANCE = "my-spring-ehcache";
    private final CacheManager cacheManager;
    private final net.sf.ehcache.CacheManager ehCacheManager;


    public CachingConfig() {
        CacheConfiguration cacheConfiguration = new CacheConfiguration();
        cacheConfiguration.setName(USER_CACHE_INSTANCE);
        cacheConfiguration.setMemoryStoreEvictionPolicy("LRU");
        cacheConfiguration.setMaxEntriesLocalHeap(1000);
        net.sf.ehcache.config.Configuration config
                = new net.sf.ehcache.config.Configuration();
        config.addCache(cacheConfiguration);
        this.ehCacheManager = net.sf.ehcache.CacheManager.newInstance(config);
        this.cacheManager = new EhCacheCacheManager(ehCacheManager);
    }

    @Bean(destroyMethod="shutdown")
    public net.sf.ehcache.CacheManager ehCacheManager() {
        return ehCacheManager;
    }

    @Bean
    @Override
    public CacheManager cacheManager() {
        return cacheManager;
    }

    @Bean
    @Override
    public KeyGenerator keyGenerator() {
        return new SimpleKeyGenerator();
    }

    @Bean
    @Override
    public CacheResolver cacheResolver() {
        return new SimpleCacheResolver(cacheManager);
    }

    @Bean
    @Override
    public CacheErrorHandler errorHandler() {
        return new SimpleCacheErrorHandler();
    }
}
 类似资料:
  • 问题内容: 在Spring 4或Spring Boot中,有没有办法在没有xml的情况下初始化EhCache? 我注意到Spring Boot 1.0.0.RC3没有任何ehcache依赖项,但是Spring 4.0GA发行版中 提到它改进了对EhCache的支持。而且,Spring 3拥有类,但是它不再是依赖项的一部分。 编辑: Spring 4确实具有EhCache支持。您必须添加依赖项: E

  • 我正在使用Spring4 AsyncRestTemplate调用外部REST API服务。 在该方法中,我注册来自REST API调用的ListenableFuture响应的回调。 除了单元测试,我不使用返回的ListenableFuture。回调将根据请求的成功或失败来处理我想采取的实际操作。 ExternalServiceImpl.class

  • 我有一个spring boot项目,以下是我使用记录器的方式: 和src/main/resources/log4j2.xml 这是我在控制台中看到的: 2019-09-10 11:04:08.818错误21680---[nio-8081-exec-2]com.ey.web.MyController: testtestBLI 如何强制log4j2使用xml文件? 分级文件:

  • 我对log4j相当陌生。正在尝试使用log4j2。要配置的xml。我将该文件添加到构建路径,它工作了一次,但不再工作。 这是我的log4j2.xml: 我把它拆下来,只是为了测试它,让它工作。我检查了调试,并且记录器配置使用默认值。 我还试着给文件命名log4j2-test.xml. 有什么想法吗? 谢啦

  • 有人能通过xml使用Spring < code > JCacheCacheManager 创建一个Ehcache支持的< code>CacheManager吗?我有这样的东西。不知道如何为Ehcache3创建< code > javax . cache . cache manager 。 谢谢!

  • 我使用的xml配置spring4工作得很好。像这样: 当我在没有xml的情况下配置spring4时。像这样: } 它引发异常: