相关疑难解决方法(0)

如何在spring cache java中配置多个缓存管理器

我希望在我的Web应用程序中配置多个Spring缓存管理器,并且我可以在项目的不同位置使用不同的缓存管理器.有没有办法做到这一点.

java spring caching spring-boot spring-cache

22
推荐指数
2
解决办法
2万
查看次数

在@PostConstruct期间使用@Cacheable的Spring缓存不起作用

与spring框架中的提交有关https://github.com/spring-projects/spring-framework/commit/5aefcc802ef05abc51bbfbeb4a78b3032ff9eee3

初始化设置为从afterPropertiesSet()到afterSingletonsInstantiated()的后续阶段

简而言之:这可以防止在@PostConstruct用例中使用缓存时缓存工作.

更长的版本:这可以防止您使用的用例

  1. 在methodB上使用@Cacheable创建serviceB

  2. 使用@PostConstruct调用serviceB.methodB创建serviceA

    @Component 
    public class ServiceA{
    
    @Autowired
    private ServiceB serviceB;
    
    @PostConstruct
    public void init() {
        List<String> list = serviceB.loadSomething();
    }
    
    Run Code Online (Sandbox Code Playgroud)

这导致org.springframework.cache.interceptor.CacheAspectSupport现在不进行初始化,因此不会缓存结果.

protected Object execute(CacheOperationInvoker invoker, Object target, Method method, Object[] args) {
   // check whether aspect is enabled
   // to cope with cases where the AJ is pulled in automatically
   if (this.initialized) {
//>>>>>>>>>>>> NOT Being called
      Class<?> targetClass = getTargetClass(target);
      Collection<CacheOperation> operations = getCacheOperationSource().getCacheOperations(method, targetClass);
      if (!CollectionUtils.isEmpty(operations)) { …
Run Code Online (Sandbox Code Playgroud)

java postconstruct spring-cache

6
推荐指数
2
解决办法
3247
查看次数