JCacheManagerCustomizer在Spring启动时不起作用

时间:2018-04-07 16:08:54

标签: hibernate spring-boot ehcache

我有一个使用缓存进行Hibernate二级缓存的spring boot和hibernate应用程序。我的缓存是通过JSR107配置创建的,如下所示。在迁移到Spring boot 2之前,这是有效的。但迁移后,cacheManagerCustomizer()被正确调用,但返回customize()上的JCacheManagerCustomizer

@Configuration
@EnableCaching
@AutoConfigureBefore(value = {WebConfigurer.class, DatabaseConfiguration.class})
public class CacheConfiguration {

    private final Logger log = LoggerFactory.getLogger(CacheConfiguration.class);
    private final javax.cache.configuration.Configuration<Object, Object> jcacheConfiguration;

    @PreDestroy
    public void destroy() {
        log.info("Remove Cache Manager metrics");
        log.info("Closing Cache Manager");
    }

    public CacheConfiguration(JHipsterProperties jHipsterProperties) {
        JHipsterProperties.Cache.Ehcache ehcache = jHipsterProperties.getCache().getEhcache();

        jcacheConfiguration = Eh107Configuration.fromEhcacheCacheConfiguration(
                CacheConfigurationBuilder.newCacheConfigurationBuilder(Object.class, Object.class,
                        ResourcePoolsBuilder.heap(ehcache.getMaxEntries()))
                        .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(java.time.Duration.ofSeconds(ehcache.getTimeToLiveSeconds())))
                        .build());
    }

    @Bean
    public JCacheManagerCustomizer cacheManagerCustomizer() {
        log.debug("Starting Ehcache");

        return cm -> {
            log.debug("Start  Customize"); // not called

            cm.createCache("baseInfoCache", jcacheConfiguration);
            cm.createCache("attachments", jcacheConfiguration);

        };
    }

}

2 个答案:

答案 0 :(得分:1)

它应该完美无缺。所以我目前的猜测是你的Spring启动依赖项存在问题。如果您已将JHipster 4迁移到Spring Boot 2,则极有可能。

另一种可能性是您的日志未设置为调试级别。你调试它以确保它被调用吗?

答案 1 :(得分:0)

我有同样的问题。我意识到我在类路径中有两个缓存提供程序(ehcache和hazelcast)。删除hazelcast后,它可以正常工作。

现在,我必须找到一种方法来定义将由JCacheManagerCustomizer自定义的自定义CacheManeger bean。