无法通过共享库将自定义缓存管理器插入 Spring 启动服务
Not able to plug-in a custom cache manager via shared library to Spring Boot Service
我正在尝试使用 Spring @cacheable 注释向服务添加缓存行为,并在 运行 时间使用自定义缓存管理器作为配置的一部分 enable/disable 缓存。
我的自定义配置 class 在服务项目中使用时工作正常。但是,当我将它作为共享库的一部分并在服务中引用它时,缓存行为不起作用。
所以,我有一个共享库,现在我的服务引用了它。我想使用共享库,以便我可以通过更改为其他缓存管理器来更改 cacheManager 策略,而不影响服务客户端。
但是,现在缓存不再 work.Within 它工作的相同服务,而是不作为共享库。
我不确定是否自动配置了其他一些缓存配置。
我想了解是否需要排除某些缓存配置 classes 或在创建其他配置之前配置我的配置。
我的配置class如下。我正在使用由 (,) 分隔的字符串对象在应用程序配置的 class 中创建缓存。
@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;
}
}
}
我正在使用标准 spring 引导自动配置方法,方法是在我的共享库组件的路径 src/main/resources/META-INF/spring.factories 中添加以下条目。
**org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.demo.autoconfigure.MyCustomCacheConfig**
因此,我希望服务启动时配置会在启动时被拾取。我将库添加为服务的依赖项 pom.xml
由于我有多个服务,我想在 运行 时具有切换缓存 on/off 的功能,我想我可以将配置作为共享库。
我还在下面的 git 存储库中共享了完整的存储库,包括服务、缓存组件、演示配置服务和文件库存储库,
https://github.com/bsridhar123/cache-demo-repo
谁能帮我找出问题的原因。
我找到了问题的解决方案。
当我在共享库组件的 application.properties 中添加如下条目并构建它并在我的服务中引用它时,它工作正常。
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration
我还删除了 @AutoConfigureBefore 我之前在配置 class ins 共享库中的注释。
似乎明确排除了自动配置 class 而不是使用注释作品,尽管我不得不在我的库中添加 application.properties。
在我的例子中,它不起作用,除非我将 application.properties 设置为:
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
我正在尝试使用 Spring @cacheable 注释向服务添加缓存行为,并在 运行 时间使用自定义缓存管理器作为配置的一部分 enable/disable 缓存。
我的自定义配置 class 在服务项目中使用时工作正常。但是,当我将它作为共享库的一部分并在服务中引用它时,缓存行为不起作用。
所以,我有一个共享库,现在我的服务引用了它。我想使用共享库,以便我可以通过更改为其他缓存管理器来更改 cacheManager 策略,而不影响服务客户端。
但是,现在缓存不再 work.Within 它工作的相同服务,而是不作为共享库。
我不确定是否自动配置了其他一些缓存配置。
我想了解是否需要排除某些缓存配置 classes 或在创建其他配置之前配置我的配置。
我的配置class如下。我正在使用由 (,) 分隔的字符串对象在应用程序配置的 class 中创建缓存。
@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;
}
}
}
我正在使用标准 spring 引导自动配置方法,方法是在我的共享库组件的路径 src/main/resources/META-INF/spring.factories 中添加以下条目。
**org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.demo.autoconfigure.MyCustomCacheConfig**
因此,我希望服务启动时配置会在启动时被拾取。我将库添加为服务的依赖项 pom.xml
由于我有多个服务,我想在 运行 时具有切换缓存 on/off 的功能,我想我可以将配置作为共享库。
我还在下面的 git 存储库中共享了完整的存储库,包括服务、缓存组件、演示配置服务和文件库存储库,
https://github.com/bsridhar123/cache-demo-repo
谁能帮我找出问题的原因。
我找到了问题的解决方案。
当我在共享库组件的 application.properties 中添加如下条目并构建它并在我的服务中引用它时,它工作正常。
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration
我还删除了 @AutoConfigureBefore 我之前在配置 class ins 共享库中的注释。
似乎明确排除了自动配置 class 而不是使用注释作品,尽管我不得不在我的库中添加 application.properties。
在我的例子中,它不起作用,除非我将 application.properties 设置为:
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration