java.lang.ClassNotFoundException: org.springframework.cache.guava.GuavaCache

java.lang.ClassNotFoundException: org.springframework.cache.guava.GuavaCache

上下文 -

我有一个 spring 引导应用程序和 我将 spring-boot-starter-parent 版本从 1.5.12.RELEASE 更新为 2.4.0

我的 pom 中有这种依赖 -

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>30.1-android</version>
</dependency>

现在当我 运行 我的应用程序时,我收到这个错误 -

    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cachesEndpoint' defined in class path resource ...

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'guavaCacheManager' defined in class path resource...

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cache.CacheManager]: Factory method 'guavaCacheManager' threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/cache/guava/GuavaCache ...

Caused by: java.lang.NoClassDefFoundError: org/springframework/cache/guava/GuavaCache
    at biz.kaar.common.services.cache.TempGuavaCacheConfig.guavaCacheManager(TempGuavaCacheConfig.java:32)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
    ... 43 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.springframework.cache.guava.GuavaCache
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:636)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:182)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:519)
    ... 49 common frames omitted

通过对网络的更多研究,我能够通过向项目添加 spring-context-support 来修复我的项目中发生的相同异常。 我通过向我的 build.gradle:

添加以下依赖项来做到这一点
compile("org.springframework:spring-context-support:4.1.7.RELEASE")

spring-context-support 是一个包含辅助方法和 类 用于配置缓存的库。

这是我关注的博客: https://codedevstuff.blogspot.com/2015/07/add-guava-cache-to-spring-boot-to-cache.html

我通过从我的项目中完全删除 guava 依赖项及其引用来解决这个问题。

如果您仍然需要使用缓存,请尝试使用 Caffeine。