由于找不到 CacheAspectSupport,CacheAutoConfiguration 不工作

CacheAutoConfiguration is not working due to CacheAspectSupport not found

我正在使用 springboot 2.1.1。 我使用 @EnableCaching

启用了缓存

CacheManager 已创建,可以注入我的 classes。

一旦我添加了一个库https://github.com/MarcGiffing/bucket4j-spring-boot-starter 启动应用程序时出现错误:

java.lang.IllegalStateException: No CacheResolver specified, and no bean of type CacheManager found. Register a CacheManager bean or remove the @EnableCaching annotation from your configuration.
at org.springframework.cache.interceptor.CacheAspectSupport.afterSingletonsInstantiated(CacheAspectSupport.java:227)

经过大量调试,我找不到库破坏 CacheManager 的原因和方式。

似乎没有使用 CacheAutoConfiguration spring class :

   CacheAutoConfiguration:
  Did not match:
     - @ConditionalOnBean (types: org.springframework.cache.interceptor.CacheAspectSupport; SearchStrategy: all) did not find any beans of type org.springframework.cache.interceptor.CacheAspectSupport (OnBeanCondition)
  Matched:
     - @ConditionalOnClass found required class 'org.springframework.cache.CacheManager' (OnClassCondition)

但是我在 ProxyCachingConfiguration#L63 中添加了一个断点,并且创建了一个 CacheInterceptor 实例(CacheAspectSupport impl)。 我可以将它注入我的配置之一 class。所以 bean CacheAspectSupport 似乎存在于应用程序上下文中。

那么为什么 CacheAutoConfiguration 说缺少 bean?

谢谢

我找到原因了。

我的配置 class 添加库时加载 @EnableCaching 注释太晚了。

我添加了一个@AutoConfigureBefore(CacheAutoConfiguration.class),现在可以使用了。