Why isn't the Bean found? (Spring 4.x EhCache 3.x, Apache Tomcat 7)

走远了吗. 提交于 2020-12-15 01:47:12

问题


Trying to add a cache to an existing Spring 4.3 (not SpringBoot) application

Using EhCache 3.5, It is confusing.

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>

in a class,

@Autowired
MyCacheService myCacheService;

in a method if the class,

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

error:

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

UPDATE:

I changed the version of ehcache to 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:


The error was due to xmlns improperly defined. When I changed it to this, it was fine.

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

and referred to it as 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>


来源:https://stackoverflow.com/questions/64868971/why-isnt-the-bean-found-spring-4-x-ehcache-3-x-apache-tomcat-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!