콩을 찾을 수없는 이유는 무엇입니까? (Spring 4.x EhCache 3.x, Apache Tomcat 7)

Nov 16 2020

기존 스프링 4.3 캐시를 추가하려고 노력하는 ( 하지 SpringBoot) 응용 프로그램

EhCache 3.5를 사용하면 혼란 스럽습니다.

beans.xml

<bean id="ehCacheManager" class="org.springframework.cache.jcache.JCacheCacheManager">
    <property name="cacheManager">
        <bean class="org.springframework.cache.jcache.JCacheManagerFactoryBean"
              jsr107:cacheManagerUri="classpath:ehcache.xml"/>
    </property>
</bean>

MyCacheService.java

package com.me;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.jcache.JCacheCacheManager;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.net.URISyntaxException;

@Component
public class MyCacheService {
    private static int value = 7;

    @Cacheable("cache")
    public int incValue() {
        return value++;
    }
}

pom.xml

    <dependency>
        <groupId>org.ehcache</groupId>
        <artifactId>ehcache</artifactId>
        <version>3.5.0</version>
    </dependency>
    <dependency>
        <groupId>javax.cache</groupId>
        <artifactId>cache-api</artifactId>
        <version>1.1.0</version>
    </dependency>

ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<config
        xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
        xmlns:jsr107='http://www.ehcache.org/v3/jsr107'
        xmlns='http://www.ehcache.org/v3'
        xsi:schemaLocation="
        http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.4.xsd
        http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.4.xsd">

  <service>
    <jsr107:defaults enable-management="false" enable-statistics="true"/>
  </service>

  <cache alias="cache">
    <resources>
      <heap unit="entries">2000</heap>
    </resources>
  </cache>
</config>

수업에서

@Autowired
MyCacheService myCacheService;

클래스의 경우 메서드에서

        System.out.println("FOO -------------" + myCacheService.incValue());

오류:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ehCacheManager' defined in ServletContext resource [/WEB-INF/cxf-beans.xml]: Cannot create inner bean 'org.springframework.cache.jcache.JCacheManagerFactoryBean#4b6fd0' of type [org.springframework.cache.jcache.JCacheManagerFactoryBean] while setting bean property 'cacheManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cache.jcache.JCacheManagerFactoryBean#4b6fd0' defined in ServletContext resource [/WEB-INF/cxf-beans.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.terracotta.statistics.StatisticsManager.createPassThroughStatistic(Ljava/lang/Object;Ljava/lang/String;Ljava/util/Set;Lorg/terracotta/statistics/StatisticType;Ljava/util/function/Supplier;)V
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:313)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:122)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1284)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351) ... 62 more Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cache.jcache.JCacheManagerFactoryBean#4b6fd0' defined in ServletContext resource [/WEB-INF/cxf-beans.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.terracotta.statistics.StatisticsManager.createPassThroughStatistic(Ljava/lang/Object;Ljava/lang/String;Ljava/util/Set;Lorg/terracotta/statistics/StatisticType;Ljava/util/function/Supplier;)V at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1634) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:299) ... 72 more Caused by: java.lang.NoSuchMethodError: org.terracotta.statistics.StatisticsManager.createPassThroughStatistic(Ljava/lang/Object;Ljava/lang/String;Ljava/util/Set;Lorg/terracotta/statistics/StatisticType;Ljava/util/function/Supplier;)V at org.ehcache.impl.internal.store.heap.OnHeapStore.<init>(OnHeapStore.java:265) at org.ehcache.impl.internal.store.heap.OnHeapStore$Provider.createStoreInternal(OnHeapStore.java:1612)
    at org.ehcache.impl.internal.store.heap.OnHeapStore$Provider.createStore(OnHeapStore.java:1579) at org.ehcache.impl.internal.store.heap.OnHeapStore$Provider.createStore(OnHeapStore.java:1560)
    at org.ehcache.core.EhcacheManager.getStore(EhcacheManager.java:506)
    at org.ehcache.core.EhcacheManager.createNewEhcache(EhcacheManager.java:316)
    at org.ehcache.core.EhcacheManager.createCache(EhcacheManager.java:265)
    at org.ehcache.core.EhcacheManager.init(EhcacheManager.java:579)
    at org.ehcache.jsr107.EhcacheCachingProvider.createCacheManager(EhcacheCachingProvider.java:151)
    at org.ehcache.jsr107.EhcacheCachingProvider.getCacheManager(EhcacheCachingProvider.java:127)
    at org.ehcache.jsr107.EhcacheCachingProvider.getCacheManager(EhcacheCachingProvider.java:78)
    at org.springframework.cache.jcache.JCacheManagerFactoryBean.afterPropertiesSet(JCacheManagerFactoryBean.java:77)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1692)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1630)
    ... 75 more

최신 정보:

ehcache 버전을 3.0.0으로 변경했습니다.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myCacheService' defined in file [C:\Foo\target\foo-web-service\WEB-INF\classes\com\me\MyCacheService.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cache.interceptor.CacheInterceptor#0': Cannot resolve reference to bean 'ehCacheManager' while setting bean property 'cacheManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ehCacheManager': Failed to introspect bean class [org.springframework.cache.jcache.JCacheCacheManager] for lookup method metadata: could not find class that it depends on; nested exception is java.lang.NoClassDefFoundError: javax/cache/CacheManager

답변

1 likejudo Nov 17 2020 at 14:56

이 오류는 xmlns가 잘못 정의 되었기 때문입니다. 이것을 이것으로 바꾸면 괜찮 았습니다.

xmlns:p="http://www.springframework.org/schema/p"

그리고 그것을 p:

<cache:annotation-driven cache-manager="ehCacheManager"/>
<bean id="ehCacheManager" class="org.springframework.cache.jcache.JCacheCacheManager">
    <property name="cacheManager">
        <bean class="org.springframework.cache.jcache.JCacheManagerFactoryBean"
              p:cacheManagerUri="classpath:ehcache.xml"/>
    </property>
</bean>