0

我有一个常见的缓存模块,它具有 spring boot 启动器缓存(版本 2.2.4.RELEASE),并且缓存具有 ehcache 和咖啡因的依赖项。下面是pom文件

    <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>

    <dependency>
        <groupId>javax.cache</groupId>
        <artifactId>cache-api</artifactId>
    </dependency>
    <dependency>
        <groupId>org.ehcache</groupId>
        <artifactId>ehcache</artifactId>
    </dependency>
    <dependency>
        <groupId>com.github.ben-manes.caffeine</groupId>
        <artifactId>caffeine</artifactId>
    </dependency>
</dependencies>

在这个模块中,自动装配了 CacheManger 并使用 spring 缓存具有缓存更新和获取方法。

@Autowired
private CacheManager cacheManager;

此模块作为依赖项添加到作为 springboot 应用程序的 app1 中,并且根据属性“spring.cache.type”,当 app1 启动时,它将初始化相应的缓存

对于咖啡因缓存

spring.cache.type=caffeine
spring.cache.caffeine.spec=maximumSize=10000

对于 Ehcache3

spring.cache.type=caffeine
spring.cache.jcache.provider=org.ehcache.jsr107.EhcacheCachingProvider
spring.cache.jcache.config=classpath:ehcache.xml

这对于s​​pringboot应用程序app1来说一切正常。

现在我在 app2 中使用相同的缓存模块,它的功能与 app1 相同,但它不是 springboot 应用程序。在 app2 中使用 @Configuration 、 @ComponentScan 来解决依赖关系。现在 app2 被用在 app3 中,它是 springboot 应用程序。除了 cacheManger 之外,所有其他依赖项都已解决并在 app3 中正常工作。运行 app3 时出现以下错误

*\r\nAPPLICATION FAILED TO START\r\n***************************\r\n\r\nDescription:\r\n\r\nField cacheManager in commonCacheService required a bean of type 'org.springframework.cache.CacheManager' that could not be found.\r\n\r\nThe injection point has the following annotations:\r\n\t- @org.springframework.beans.factory.annotation.Autowired(required=true)\r\n\r\nThe following candidates were found but could not be injected:\r\n\t- Bean method 'cacheManager' in 'EhCacheCacheConfiguration' not loaded because @ConditionalOnClass did not find required class 'net.sf.ehcache.Cache'\r\n\t- Bean method 'cacheManager' in 'GenericCacheConfiguration' not loaded because Cache org.springframework.boot.autoconfigure.cache.GenericCacheConfiguration unknown cache type\r\n\t- Bean method 'cacheManager' in 'JCacheCacheConfiguration' not loaded because Cache org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration unknown cache type\r\n\t- Bean method 'cacheManager' in 'NoOpCacheConfiguration' not loaded because Cache org.springframework.boot.autoconfigure.cache.NoOpCacheConfiguration unknown cache type\r\n\t- Bean method 'cacheManager' in 'SimpleCacheConfiguration' not loaded because Cache org.springframework.boot.autoconfigure.cache.SimpleCacheConfiguration unknown cache type\r\n\r\n\r\nAction:\r\n\r\nConsider revisiting the entries above or defining a bean of type 'org.springframework.cache.CacheManager' in your configuration.\r\n"}

如果我在公共缓存模块中创建 bean,如下所示

@Bean
@ConditionalOnProperty(name="spring.cache.type", havingValue="caffeine")
@Primary
public CacheManager cacheManager() {
    CaffeineCacheManager cacheManager = new CaffeineCacheManager();
    cacheManager.setCaffeine(Caffeine.newBuilder().maximumSize(125000));
    return cacheManager;
}

但我想避免在公共模块中创建 bean。app2 中是否需要任何注释/配置(不更改 app3,因为在 app3 上没有任何更新控制),以便它也可以像在 app1 中一样工作)?

4

1 回答 1

0

我知道现在已经很晚了,但我遇到了同样的问题。通过添加依赖解决了

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
    <groupId>javax.cache</groupId>
     <artifactId>cache-api</artifactId>
     <version>1.1.1</version>
</dependency>

并添加

@EnableCaching

在 SpringBootApplciation 类中

于 2022-02-10T13:18:03.283 回答