如何解决不使用@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 举报,一经查实,本站将立刻删除。