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

Spring+Hibernate版本升级后JBoss部署出现Ehcache及Security异常

Spring+Hibernate版本升级JBoss部署异常解决方案

版本升级信息

  • 原使用版本:
    <spring.version>3.0.6.RELEASE</spring.version>
    <spring.security.version>3.0.8.RELEASE</spring.security.version>
    <hibernate.version>3.3.0.GA</hibernate.version>
    
  • 升级目标版本:
    <spring.version>5.3.31</spring.version>
    <spring.security.version>5.8.9</spring.security.version>
    <hibernate.version>5.6.15.Final</hibernate.version>
    

部署异常信息

部署JBoss服务器时触发异常,具体错误如下:

org.springframework.beans.factory.BeanCreationException: 创建在ServletContext资源[/WEB-INF/classes/applicationContext.xml]中定义的名为'EhcacheManager'的Bean失败:通过构造函数实例化Bean失败;嵌套异常是org.springframework.beans.BeanInstantiationException: 实例化[net.sf.ehcache.CacheManager]失败:构造函数抛出异常;嵌套异常是net.sf.ehcache.CacheException: 同一VM中已存在另一个未命名的CacheManager。请为配置中的每个CacheManager提供唯一名称,或执行以下操作之一:
1. 使用CacheManager.create()静态工厂方法之一,重用同名的CacheManager,或在必要时创建一个
2. 在创建同名的新CacheManager之前关闭之前的cacheManager。

现有CacheManager的来源是:InputStreamConfigurationSource [stream=java.io.FileInputStream@4c572339]
service jboss.undertow.deployment.default-server.default-host./repository: java.lang.RuntimeException: org.springframework.beans.factory.BeanCreationException: 创建名为'org.springframework.security.filterChains'的Bean失败:设置Bean属性'sourceList'的键[0]时无法解析对Bean 'org.springframework.security.web.DefaultSecurityFilterChain#0'的引用;嵌套异常是org.springframework.beans.factory.BeanCreationException: 创建名为'org.springframework.security.web.DefaultSecurityFilterChain#0'的Bean失败:设置时无法创建类型为[org.springframework.web.filter.CorsFilter]的内部Bean '(inner bean)#3f48e503'

问题分析

核心问题是同一JVM内存在多个未命名的Ehcache CacheManager实例,新版本Spring/Hibernate整合Ehcache时,默认实例化逻辑与旧版本存在差异,叠加JBoss的类加载机制,导致重复创建匿名CacheManager。后续的CorsFilter创建失败是CacheManager初始化失败引发的连锁反应,解决CacheManager冲突后可自动修复。

解决步骤

1. 为Ehcache配置指定唯一名称

修改项目中的ehcache.xml配置文件,在根节点<ehcache>上添加唯一名称属性,避免匿名实例冲突:

<ehcache name="YourAppUniqueCacheName" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd">
    <!-- 原有缓存配置内容 -->
</ehcache>

2. 改用Spring提供的EhCacheManagerFactoryBean管理实例

在applicationContext.xml中,替换直接实例化net.sf.ehcache.CacheManager的配置,使用Spring封装的工厂类,它会自动处理单例和命名冲突:

<bean id="EhcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation" value="classpath:ehcache.xml"/>
    <property name="cacheManagerName" value="YourAppUniqueCacheName"/>
</bean>

3. 隔离JBoss全局类加载的Ehcache依赖

如果JBoss服务器全局部署了Ehcache库,会引发类版本冲突。在项目中添加jboss-deployment-structure.xml文件,配置依赖隔离:

<jboss-deployment-structure>
    <deployment>
        <exclusions>
            <module name="net.sf.ehcache"/>
        </exclusions>
    </deployment>
</jboss-deployment-structure>

4. 确保CacheManager正确销毁

如果仍需手动实例化CacheManager,需配置销毁方法,保证Spring上下文关闭时正确关闭缓存管理器:

<bean id="EhcacheManager" class="net.sf.ehcache.CacheManager" destroy-method="shutdown">
    <constructor-arg value="classpath:ehcache.xml"/>
</bean>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 00:45:29