springboot(已整合redis)整合JetCache2.7版本,CacheManager注入失败

ohtdti5x  于 2022-11-02  发布在  Spring
关注(0)|答案(8)|浏览(806)

打扰,请教个问题:
springboot(之前整合过redis)整合2.7.0.RC3版本lettuce
pom:

<dependency>
            <groupId>com.alicp.jetcache</groupId>
            <artifactId>jetcache-redis-lettuce</artifactId>
            <version>${jetcache.latest.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alicp.jetcache</groupId>
            <artifactId>jetcache-starter-redis-lettuce</artifactId>
            <version>${jetcache.latest.version}</version>
        </dependency>

启动服务异常:
The bean 'cacheManager', defined in class path resource [org/springframework/boot/autoconfigure/cache/JCacheCacheConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [com/alicp/jetcache/autoconfigure/JetCacheAutoConfiguration.class] and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

应该是引入springboot的redis依赖导致:

<!--redis-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

这个该如何处理?

js4nwp54

js4nwp541#

和spring cache名字冲突了是吗,我看看啊

5t7ly7z5

5t7ly7z52#

我有个提交改了bean名字,你clone下来用master分支自己build一个试试?版本是2.7.0-SNAPSHOT

mvn -DskipTests clean install

ewm0tg9j

ewm0tg9j3#

用了 2.7.0.RC3 一段时间了,没有发现 CacheManager 注入失败问题,用得好好的。感觉 CacheManager 注入失败相关的ISSUE,大多都和自身的环境有关系。依赖其它的starter,自身环境的模块逻辑以及Bean的注入顺序和逻辑都会有影响。

我的代码供参考:

@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(CacheProperties.class)
@Import({CaffeineConfiguration.class, RedisConfiguration.class})
@AutoConfigureAfter(JetCacheAutoConfiguration.class)
public class JetCacheConfiguration {

    private static final Logger log = LoggerFactory.getLogger(JetCacheConfiguration.class);

    @PostConstruct
    public void postConstruct() {
        log.debug("[Herodotus] |- SDK [Engine Cache JetCache] Auto Configure.");
    }

    @Bean
    @ConditionalOnClass(CacheManager.class)
    public JetCacheCreateCacheFactory jetCacheCreateCacheFactory(CacheManager cacheManager) {
        JetCacheCreateCacheFactory factory = new JetCacheCreateCacheFactory(cacheManager);
        JetCacheUtils.setJetCacheCreateCacheFactory(factory);
        log.trace("[Herodotus] |- Bean [Jet Cache Create Cache Factory] Auto Configure.");
        return factory;
    }

    @Bean
    @Primary
    @ConditionalOnMissingBean
    public HerodotusCacheManager herodotusCacheManager(JetCacheCreateCacheFactory jetCacheCreateCacheFactory, CacheProperties cacheProperties) {
        HerodotusCacheManager herodotusCacheManager = new HerodotusCacheManager(jetCacheCreateCacheFactory, cacheProperties);
        herodotusCacheManager.setAllowNullValues(cacheProperties.getAllowNullValues());
        log.trace("[Herodotus] |- Bean [Jet Cache Herodotus Cache Manager] Auto Configure.");
        return herodotusCacheManager;
    }
}
vm0i2vca

vm0i2vca4#

用了 2.7.0.RC3 一段时间了,没有发现 CacheManager 注入失败问题,用得好好的。感觉 CacheManager 注入失败相关的ISSUE,大多都和自身的环境有关系。依赖其它的starter,自身环境的模块逻辑以及Bean的注入顺序和逻辑都会有影响。

我的代码供参考:

@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(CacheProperties.class)
@Import({CaffeineConfiguration.class, RedisConfiguration.class})
@AutoConfigureAfter(JetCacheAutoConfiguration.class)
public class JetCacheConfiguration {

    private static final Logger log = LoggerFactory.getLogger(JetCacheConfiguration.class);

    @PostConstruct
    public void postConstruct() {
        log.debug("[Herodotus] |- SDK [Engine Cache JetCache] Auto Configure.");
    }

    @Bean
    @ConditionalOnClass(CacheManager.class)
    public JetCacheCreateCacheFactory jetCacheCreateCacheFactory(CacheManager cacheManager) {
        JetCacheCreateCacheFactory factory = new JetCacheCreateCacheFactory(cacheManager);
        JetCacheUtils.setJetCacheCreateCacheFactory(factory);
        log.trace("[Herodotus] |- Bean [Jet Cache Create Cache Factory] Auto Configure.");
        return factory;
    }

    @Bean
    @Primary
    @ConditionalOnMissingBean
    public HerodotusCacheManager herodotusCacheManager(JetCacheCreateCacheFactory jetCacheCreateCacheFactory, CacheProperties cacheProperties) {
        HerodotusCacheManager herodotusCacheManager = new HerodotusCacheManager(jetCacheCreateCacheFactory, cacheProperties);
        herodotusCacheManager.setAllowNullValues(cacheProperties.getAllowNullValues());
        log.trace("[Herodotus] |- Bean [Jet Cache Herodotus Cache Manager] Auto Configure.");
        return herodotusCacheManager;
    }
}

方便发下jetcache的依赖吗,我正在验证你的方法,但发现JetCacheCreateCacheFactory和JetCacheUtils未找到对应依赖

t9aqgxwy

t9aqgxwy5#

我有个提交改了bean名字,你clone下来用master分支自己build一个试试?版本是2.7.0-SNAPSHOT

mvn -DskipTests clean install

重新依赖修改后的SNAPSHOT版本解决了,应该是Springboot中CacheManager和JetCache中CacheManager重名导致

fwzugrvs

fwzugrvs6#

我有个提交改了bean名字,你clone下来用master分支自己build一个试试?版本是2.7.0-SNAPSHOT

mvn -DskipTests clean install

非常感谢,希望合并到后续的release版本中,方便无缝升级

goucqfw6

goucqfw68#

用了 2.7.0.RC3 一段时间了,没有发现 CacheManager 注入失败问题,用得好好的。感觉 CacheManager 注入失败相关的ISSUE,大多都和自身的环境有关系。依赖其它的starter,自身环境的模块逻辑以及Bean的注入顺序和逻辑都会有影响。
我的代码供参考:

@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(CacheProperties.class)
@Import({CaffeineConfiguration.class, RedisConfiguration.class})
@AutoConfigureAfter(JetCacheAutoConfiguration.class)
public class JetCacheConfiguration {

    private static final Logger log = LoggerFactory.getLogger(JetCacheConfiguration.class);

    @PostConstruct
    public void postConstruct() {
        log.debug("[Herodotus] |- SDK [Engine Cache JetCache] Auto Configure.");
    }

    @Bean
    @ConditionalOnClass(CacheManager.class)
    public JetCacheCreateCacheFactory jetCacheCreateCacheFactory(CacheManager cacheManager) {
        JetCacheCreateCacheFactory factory = new JetCacheCreateCacheFactory(cacheManager);
        JetCacheUtils.setJetCacheCreateCacheFactory(factory);
        log.trace("[Herodotus] |- Bean [Jet Cache Create Cache Factory] Auto Configure.");
        return factory;
    }

    @Bean
    @Primary
    @ConditionalOnMissingBean
    public HerodotusCacheManager herodotusCacheManager(JetCacheCreateCacheFactory jetCacheCreateCacheFactory, CacheProperties cacheProperties) {
        HerodotusCacheManager herodotusCacheManager = new HerodotusCacheManager(jetCacheCreateCacheFactory, cacheProperties);
        herodotusCacheManager.setAllowNullValues(cacheProperties.getAllowNullValues());
        log.trace("[Herodotus] |- Bean [Jet Cache Herodotus Cache Manager] Auto Configure.");
        return herodotusCacheManager;
    }
}

方便发下jetcache的依赖吗,我正在验证你的方法,但发现JetCacheCreateCacheFactory和JetCacheUtils未找到对应依赖

这两个类是方便自己使用jetcache,用于实现使用jetcache作为JPA的二级缓存,就是用jetcache 2.7.0 QuickConfig 方式构造缓存。从使用jetcache的Angular 可以忽略不计,这里只是为了说明注入 jetcache CacheManager我这里是没有问题的。

我的代码中就仅是依赖了:

<dependency>
            <groupId>com.alicp.jetcache</groupId>
            <artifactId>jetcache-starter-redis-lettuce</artifactId>
        </dependency>

相关代码在: https://gitee.com/herodotus/dante-engine 工程的,engine-cache --> cache-sdk-jetcache 的包中,感兴趣可以看一下。

相关问题