Java 无法通过共享库将自定义缓存管理器插入到Spring引导服务
我正在尝试使用Spring@cacheable注释,并使用自定义cachemanager作为配置的一部分,在运行时启用/禁用缓存,将缓存行为添加到服务中 我的自定义配置类在服务项目中使用时可以正常工作。但是,当我将其作为共享库的一部分并在服务中引用它时,缓存行为不起作用 因此,我有一个共享库,现在我的服务引用它。我想使用共享库来更改缓存管理器,这样我就可以通过更改其他缓存管理器来更改缓存管理器策略,而不会影响服务客户端 但是,现在缓存不起作用。在同一个服务中,它起作用,但不是作为一个共享库 我不确定是否自动配置了其他缓存配置 我想了解在创建其他配置之前是否需要排除某些缓存配置类或配置我的配置 我的配置类如下所示。我使用一个由(,)分隔的字符串对象从应用程序配置在类中创建缓存Java 无法通过共享库将自定义缓存管理器插入到Spring引导服务,java,caching,spring-boot,spring-cloud-config,Java,Caching,Spring Boot,Spring Cloud Config,我正在尝试使用Spring@cacheable注释,并使用自定义cachemanager作为配置的一部分,在运行时启用/禁用缓存,将缓存行为添加到服务中 我的自定义配置类在服务项目中使用时可以正常工作。但是,当我将其作为共享库的一部分并在服务中引用它时,缓存行为不起作用 因此,我有一个共享库,现在我的服务引用它。我想使用共享库来更改缓存管理器,这样我就可以通过更改其他缓存管理器来更改缓存管理器策略,而不会影响服务客户端 但是,现在缓存不起作用。在同一个服务中,它起作用,但不是作为一个共享库 我不
@Configuration
@EnableCaching
@RefreshScope
@AutoConfigureBefore(CacheAutoConfiguration.class)
public class MyCustomCacheConfig {
/* Flag to Determine if Cache should be Enabled or not for the service */
@Value("${cacheEnabled}")
private boolean cacheEnabled;
/* Name of the caches to be created */
@Value("${caches}")
private String caches;
@Bean
@Primary
@RefreshScope
CacheManager cacheManager() {
//If Cache is Enabled then swap the cacheManager for SimpleCacheManager
if (cacheEnabled) {
List<String> cacheNameList = Arrays.asList(caches.split(","));
SimpleCacheManager cacheManager = new SimpleCacheManager();
List<ConcurrentMapCache> conMapList = new ArrayList<>();
for (int i = 0; i < cacheNameList.size(); i++) {
conMapList.add(new ConcurrentMapCache(cacheNameList.get(i)));
}
cacheManager.setCaches(conMapList);
return cacheManager;
}else{
CacheManager cacheManager = new NoOpCacheManager();
return cacheManager;
}
}
}
因此,我希望在服务启动时,配置将在启动时被拾取。我已将库作为依赖项添加到服务pom.xml中
由于我有多个服务,我希望在运行时具有打开/关闭缓存的功能,因此我认为可以将配置作为共享库
我还共享了完整的存储库,包括服务、缓存组件、演示配置服务和下面git repo中的文件库repo
有人能帮我找出问题的原因吗。我找到了问题的解决方案 当我在共享库组件的application.properties中添加一个条目,并在我的服务中构建和引用它时,它工作得很好
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration
我还删除了先前在共享库中的配置类中的@自动配置前的注释
似乎明确排除了自动配置类,而不是使用注释,尽管我必须将application.properties添加到我的库中。在我的情况下,除非我将application.properties设置为:
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration