Spring MVC配置缓存后启动报错:无法找到CacheInterceptor
java.lang.NoClassDefFoundError: org/springframework/cache/interceptor/CacheInterceptor in Spring MVC Hey there, let's work through this issue step by step. That error you're seeing is a classic sign your application can't find the required Spring Cache classes at runtime—here's how to fix it:
1. Your Spring version doesn't support Cache functionality
Looking at your XML schema configuration, you're referencing Spring 3.0 (spring-beans-3.0.xsd, spring-context-3.0.xsd), but Spring Cache was only introduced in Spring 3.1. The CacheInterceptor class simply doesn't exist in Spring 3.0, which is why you're hitting this error.
Fix: Upgrade to Spring 3.1 or later
- Update your XML schema locations to match a supported version. For example, adjust your
xsi:schemaLocationlike this:xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd" - Ensure all your Spring dependencies (core, context, mvc, etc.) are updated to the same 3.1+ version to avoid version conflicts.
2. Missing Spring Cache dependencies
Even after upgrading to Spring 3.1+, you need to include the correct JARs for caching support. The CacheInterceptor class lives in the spring-context-support module.
If using Maven, add this dependency:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>3.1.4.RELEASE</version> <!-- Use your upgraded Spring version here --> </dependency>
If manually managing JARs:
Download spring-context-support-[version].jar and add it to your project's classpath. Make sure its version matches your other Spring JARs exactly.
3. Verify your complete cache configuration
You mentioned "其他配置已省略"—don't forget to add a valid cache manager bean, as <cache:annotation-driven /> needs one to function. A basic in-memory example looks like this:
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager"> <property name="caches"> <set> <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="myCache"/> </set> </property> </bean>
Final Checks
- Clean your project's build directory (like
targetfor Maven) to remove any old, conflicting JARs. - Rebuild and redeploy your application to ensure all new dependencies are picked up.
That should resolve the NoClassDefFoundError and get your caching setup working properly!
内容的提问来源于stack exchange,提问作者khoa_chung_89

