You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 3集成Ehcache配置启动报错求助

Spring Boot集成Ehcache 3启动报错javax/xml/bind/ValidationEventHandler问题解决

问题场景

在Spring Boot应用中配置Ehcache 3.10.2,编写了ehcache.xml配置文件和CacheConfig配置类,但启动应用时抛出NoClassDefFoundError: javax/xml/bind/ValidationEventHandler错误。

1. ehcache.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://www.ehcache.org/v3'>
    <cache alias="myCache">
        <expiry>
            <ttl unit="minutes">60</ttl>
        </expiry>
        <heap>20</heap>
    </cache>
</config>

2. CacheConfig配置类

import org.ehcache.jsr107.EhcacheCachingProvider;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.jcache.JCacheCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.cache.CacheManager;
import javax.cache.Caching;
import java.net.URI;
import java.net.URISyntaxException;

@Configuration
@EnableCaching
public class CacheConfig {
  @Bean
  public JCacheCacheManager cacheManager() throws URISyntaxException {
    ClassLoader classLoader = getClass().getClassLoader();
    URI ehcacheURI = classLoader.getResource("ehcache.xml").toURI();
    CacheManager cacheManager = Caching.getCachingProvider(EhcacheCachingProvider.class.getName()).getCacheManager(ehcacheURI, classLoader);

    return new JCacheCacheManager(cacheManager);
  }
}

核心报错信息

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cacheManager' defined in class path resource [com/webservice/config/CacheConfig.class]: Failed to instantiate [org.springframework.cache.jcache.JCacheCacheManager]: Factory method 'cacheManager' threw exception with message: javax/xml/bind/ValidationEventHandler
...
Caused by: java.lang.NoClassDefFoundError: javax/xml/bind/ValidationEventHandler
    at org.ehcache.xml.XmlConfiguration.<init>(XmlConfiguration.java:114)
...
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.ValidationEventHandler
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)

问题原因

不是ehcache.xml配置有误,是JDK版本兼容问题:

  • JAXB(XML绑定API)在JDK 8及以前是JDK核心自带的,但从JDK 9开始,JAXB被移出核心模块,成为独立依赖包。
  • Ehcache 3.10.2解析XML配置时依赖JAXB的ValidationEventHandler类,如果你用的是JDK 9+,就会出现这个类找不到的错误。

解决方案

方案1:添加JAXB依赖

在项目依赖中补充JAXB相关包:

Maven(pom.xml)

<dependencies>
    <!-- JAXB API -->
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.1</version>
    </dependency>
    <!-- JAXB实现 -->
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.3.3</version>
        <scope>runtime</scope>
    </dependency>
    <!-- JAXB核心模块 -->
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-core</artifactId>
        <version>2.3.0.1</version>
    </dependency>
    <!-- 激活模块 -->
    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>activation</artifactId>
        <version>1.1.1</version>
    </dependency>
</dependencies>

Gradle(build.gradle)

dependencies {
    implementation 'javax.xml.bind:jaxb-api:2.3.1'
    runtimeOnly 'com.sun.xml.bind:jaxb-impl:2.3.3'
    implementation 'com.sun.xml.bind:jaxb-core:2.3.0.1'
    implementation 'javax.activation:activation:1.1.1'
}

方案2:改用Java代码配置Ehcache

如果不想加额外依赖,直接用Java代码定义缓存配置,避免XML解析对JAXB的依赖:
修改CacheConfig类如下:

import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.ExpiryPolicyBuilder;
import org.ehcache.config.units.EntryUnit;
import org.ehcache.jsr107.EhcacheCachingProvider;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.jcache.JCacheCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.cache.CacheManager;
import java.time.Duration;

@Configuration
@EnableCaching
public class CacheConfig {
    @Bean
    public JCacheCacheManager cacheManager() {
        // 构建Ehcache实例
        org.ehcache.CacheManager ehcacheManager = CacheManagerBuilder.newCacheManagerBuilder()
                .withCache("myCache", CacheConfigurationBuilder.newCacheConfigurationBuilder(
                        Object.class, Object.class,
                        org.ehcache.config.builders.ResourcePoolsBuilder.newResourcePoolsBuilder()
                                .heap(20, EntryUnit.ENTRIES))
                        .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofMinutes(60)))
                )
                .build(true);

        // 转换为Spring兼容的JCacheManager
        EhcacheCachingProvider provider = (EhcacheCachingProvider) javax.cache.Caching.getCachingProvider();
        CacheManager cacheManager = provider.getCacheManager(ehcacheManager.getURI(), getClass().getClassLoader());

        return new JCacheCacheManager(cacheManager);
    }
}

方案3:降级到JDK 8

如果项目允许,直接切换回JDK 8,JDK自带JAXB模块,无需额外依赖即可正常运行。

验证

添加依赖或修改配置后,重启Spring Boot应用,缓存配置即可正常加载,不会再出现ValidationEventHandler相关错误。

内容的提问来源于stack exchange,提问作者syy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 04:16:08