NoClassDefFoundError 与新的 EHCache 配置

NoClassDefFoundError with new EHCache config

我有一个 Java class,它有一个静态实现,可以获取缓存配置并将其实例化,如下所示:

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.apache.log4j.Logger;

import java.net.URL;

public final class MyCacheImplementation {

    private MyCacheImplementation() {
        super();
    }

    private static Cache myCache;

    private static final String EHCACHFILENAME = "....... ehcache.xml";

    static {
        try {
            final URL url = MyCacheImplementation.class.getClassLoader().getResource(EHCACHFILENAME);
            final CacheManager cacheManager = new CacheManager(url);
            myCache = cacheManager.getCache("myCacheName);
        } catch (final Exception e) {
            throw new RuntimeException("Error initializing Ehcache", e);
        }
    }

    public static void addItemToCache(final Object obj) {       
        myCache.put(new Element("obj1234", obj));       
    }

    public static Cache getMyCache() {
        return myCache;
    }

}

然后我有一个 class 以静态方式引用上面的 class 并查询缓存,如下所示:

final MyCacheImplementation cacheReference = MyCacheImplementation.getMyCache();

我的缓存配置如下所示:

<ehcache>   
    <defaultCache maxElementsInMemory="10000" eternal="false"
        timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
        diskPersistent="false" diskExpiryThreadIntervalSeconds="120" />

    <cache name="myCacheName" maxElementsInMemory="1000"
        eternal="false" overflowToDisk="false" timeToIdleSeconds="18000"
        diskPersistent="false"
        timeToLiveSeconds="18000" />
</ehcache>

一切正常,我可以通过这种方式访问​​缓存。

然后我们将 EHCache 版本升级到 2.9 并关闭了光盘存储 将上面的配置更改为:

<ehcache>
    <defaultCache maxElementsInMemory="10000" timeToIdleSeconds="120" timeToLiveSeconds="120">
        <persistence strategy="none" />
    </defaultCache>

    <cache name="myCacheName" maxElementsInMemory="1000" timeToIdleSeconds="18000" timeToLiveSeconds="18000">
        <persistence strategy="none" />
    </cache>
</ehcache>

现在,如果我 运行 与上述相同,我会收到 java.lang.NoClassDefFoundError: Could not initialize class NoClassDefinition 错误。如果我将 EHCache 配置回滚到原来的状态,它就可以完美运行。知道这个配置有什么问题吗?我在我们的代码中有几个其他地方的配置完全相同并且不会导致任何问题?

事实证明,实际的异常被吞噬了。

异常是在尝试初始化缓存时出现以下错误消息:

ehcache Element <defaultCache> does not allow nested <persistence> elements

事实证明,我对 Hibernate 缓存引入的旧版本 EHCache 有传递依赖性,如 this site

所解释