为什么找不到Bean?(Spring 4.x EhCache 3.x,Apache Tomcat 7) [英] Why isn't the Bean found? (Spring 4.x EhCache 3.x, Apache Tomcat 7)

查看:175
本文介绍了为什么找不到Bean?(Spring 4.x EhCache 3.x,Apache Tomcat 7)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将缓存添加到现有的Spring 4.3( SpringBoot)应用程序

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

使用EhCache 3.5,令人困惑.

Using EhCache 3.5, It is confusing.

beans.xml

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

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

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;

在方法中(如果是类的话)

in a method if the class,

        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

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

推荐答案

该错误是由于xmlns定义不正确引起的.当我将其更改为此时,就很好了.

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"

并称为 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>

这篇关于为什么找不到Bean?(Spring 4.x EhCache 3.x,Apache Tomcat 7)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆