不使用@SpringBootApplication即可自动装配CacheManger咖啡因和Ehcache

问题描述

我有一个公共的缓存模块,该模块具有spring boot starter缓存(版本2.2.4.RELEASE),并且对于缓存具有ehcache和caffeine依赖性。下面是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

对于springboot应用程序app1,一切正常。

现在我在app2中使用相同的缓存模块,其功能与app1相同,但它不是springboot应用程序。在使用@Configuration的app2中,使用@ComponentScan解决依赖关系。现在app2正在springboot应用程序app3中使用。除了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中相同的方式工作)?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...