当前位置: 首页 > 知识库问答 >
问题:

Springboot和Ehcache-MultiCache异常

萧献
2023-03-14

我正在尝试将缓存添加到springboot应用程序中,并且我遇到了一个问题,即在启动期间抛出org.ehcache.jsr107.MultiCacheException异常

我使用的是以下(全部通过Maven pom文件加载):Springboot 1.5.5,Ehcache 3.3.1,Javax cache 1.0.0

我的SpringBootApplication看起来像这样:

@SpringBootApplication
@EnableCaching
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

我有一个包含以下内容的CacheConfig类:

@Component
public class CacheConfig implements JCacheManagerCustomizer{
    @Override
    public void customize(javax.cache.CacheManager cacheManager) {
        cacheManager.createCache("item", new MutableConfiguration<>()
                .setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.MINUTES, 5)))
                .setStoreByValue(false)
                .setStatisticsEnabled(true));
    }
}

我的 ehcache.xml 文件包含:

<config
        xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
        xmlns='http://www.ehcache.org/v3'
        xmlns:jsr107='http://www.ehcache.org/v3/jsr107'>

    <service>
        <jsr107:defaults>
            <jsr107:cache name="item" template="heap-cache"/>
        </jsr107:defaults>
    </service>

    <cache-template name="heap-cache">
        <listeners>
            <listener>
                <class>org.terracotta.ehcache.EventLogger</class>
                <event-firing-mode>ASYNCHRONOUS</event-firing-mode>
                <event-ordering-mode>UNORDERED</event-ordering-mode>
                <events-to-fire-on>CREATED</events-to-fire-on>
                <events-to-fire-on>UPDATED</events-to-fire-on>
                <events-to-fire-on>EXPIRED</events-to-fire-on>
                <events-to-fire-on>REMOVED</events-to-fire-on>
                <events-to-fire-on>EVICTED</events-to-fire-on>
            </listener>
        </listeners>
        <resources>
            <heap unit="entries">2000</heap>
            <offheap unit="MB">100</offheap>
        </resources>
    </cache-template>
</config>

测试项服务包括:

@Service
public class ItemService {

    @CacheResult(cacheName = "item")
    public String getItem(int itemNumber) {
        switch (itemNumber) {
            case 1:
                return "Item 1";
            case 2:
                return "Item 2";
            default:
                return "No Item";

        }
    }
}

最后,application.properties 包含:

spring.cache.jcache.config=classpath:ehcache.xml

当我运行该应用程序时,我得到以下以结尾的异常:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jCacheCacheManager' defined in class path resource [org/springframework/boot/autoconfigure/cache/JCacheCacheConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.cache.CacheManager]: Factory method 'jCacheCacheManager' threw exception; nested exception is org.ehcache.jsr107.MultiCacheException: [Exception 0] org.terracotta.ehcache.EventLogger
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    ... 70 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.cache.CacheManager]: Factory method 'jCacheCacheManager' threw exception; nested exception is org.ehcache.jsr107.MultiCacheException: [Exception 0] org.terracotta.ehcache.EventLogger
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    ... 83 common frames omitted
Caused by: org.ehcache.jsr107.MultiCacheException: [Exception 0] org.terracotta.ehcache.EventLogger
    at org.ehcache.jsr107.ConfigurationMerger.mergeConfigurations(ConfigurationMerger.java:138) ~[ehcache-3.3.1.jar:3.3.1 01f4b2121ef38b7e7d95c952c773881d5b1051d8]
    at org.ehcache.jsr107.Eh107CacheManager.createCache(Eh107CacheManager.java:190) ~[ehcache-3.3.1.jar:3.3.1 01f4b2121ef38b7e7d95c952c773881d5b1051d8]
    at com.example.cacheexample.CacheConfig.customize(CacheConfig.java:18) ~[classes/:na]
    at org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration.customize(JCacheCacheConfiguration.java:149) ~[spring-boot-autoconfigure-1.5.5.RELEASE.jar:1.5.5.RELEASE]
    at org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration.jCacheCacheManager(JCacheCacheConfiguration.java:104) ~[spring-boot-autoconfigure-1.5.5.RELEASE.jar:1.5.5.RELEASE]
    at org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration$$EnhancerBySpringCGLIB$$e218d7d3.CGLIB$jCacheCacheManager$1(<generated>) ~[spring-boot-autoconfigure-1.5.5.RELEASE.jar:1.5.5.RELEASE]
    at org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration$$EnhancerBySpringCGLIB$$e218d7d3$$FastClassBySpringCGLIB$$b2f1636b.invoke(<generated>) ~[spring-boot-autoconfigure-1.5.5.RELEASE.jar:1.5.5.RELEASE]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration$$EnhancerBySpringCGLIB$$e218d7d3.jCacheCacheManager(<generated>) ~[spring-boot-autoconfigure-1.5.5.RELEASE.jar:1.5.5.RELEASE]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    ... 84 common frames omitted

我不知道该怎么办,因为我几乎复制了这个网站上的发现:http://www.ehcache.org/blog/2016/05/18/ehcache3_jsr107_spring.html

任何见解将不胜感激。

谢了。

共有1个答案

汪胤
2023-03-14

原来,ehcache.xml文件中定义的org . terra cotta . ehcache . event logger类只是一个示例日志记录器,用于上面的博客文章中定义的测试程序。这个包的名字让我相信它是ehcache发行版的一部分。

当我单步执行代码时,我看到正在生成NoClassDefFoundError异常,并导致抛出MultiCacheException。堆栈跟踪中没有任何内容表明NoClassDefFoundError是真正的罪魁祸首。我创建了自己的事件记录程序,现在一切正常。

谢谢你的指点!

 类似资料:
  • > 该服务使用SpringBoot、Maven、MongoDB和Ehcache。 服务需要一个快速且频繁缓存的服务器,所以最终,我选择了Ehcache。 所有缓存都将以几乎相同的频率调用,因此在这种情况下没有热数据或冷数据。 MongoDB中的原始数据每天都会被定时器服务更新,所以我每天要做的就是将所有更新的数据加载到Ehcache中。 这些数据中的每一项都彼此有联系,就像您使用一个来查找另一个的

  • 我目前正在将一些项目转换为maven,并且在战争开始时遇到了一些问题。因此,我刚刚创建了一个非常简单的ehcache maven项目,我得到了一个类加载器类型的问题。这是我的POM: Spring Config:http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring

  • 本文向大家介绍springboot整合EHCache的实践方案,包括了springboot整合EHCache的实践方案的使用技巧和注意事项,需要的朋友参考一下  EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。   ehcache提供了多种缓存策略,主要分为内存和磁盘两级,所以无需担心容量问题。   spring-bo

  • roo-1.3.1.RC1. 我创建了具有适当 PK 和 FK 关系的简单 3-4 个表。我加载了数据。我使用spring-roo - reverse engg技术来生成基于JSF的UI。我有一个员工列表,它显示在默认的JSF生成的页面中,具有正确的分页。 如果员工数量在数据库中非常高,并且如果我想在应用程序启动期间加载到ehCache/hibernate二级缓存,那么我如何才能做到这一点? eh

  • 问题内容: 我正在使用具有磁盘存储持久性的缓存。在随后重新运行该应用程序时,出现以下错误: 除了在应用程序中的某个位置显式调用之外,还有什么方法可以解决此问题? 缓存配置: 复制问题的代码: 问题答案: 尝试设置系统属性: net.sf.ehcache.enableShutdownHook = true 因此,您可以在程序的开头添加以下行: 或者,从命令行传递属性: 注意,ehcache网站在使用

  • 我有一个使用JDK8开发的SpringBoot应用程序,现在已移植到JDK11。我在启动时遇到以下异常: 这是我的pom。xml: 我已经尝试了从2.1.8到2.2.1的SpringWebStarter,我总是得到相同的错误。 我无法再次使用JDK8,因为计算机已从Windows 7更新到Windows 10,并且我不愿意在Oracle注册以下载JDK8;) 有没有人遇到过这个问题,或者有人知道如