web-dev-qa-db-ja.com

Springフレームワークでのehcache設定

私はRSSフィードからいくつかのコンテキストをロードし、春にehcacheライブラリを使用してクライアントにキャッシュとして渡します。ここに私のコードがあります:

    import org.springframework.cache.annotation.Cacheable;
@Service
public class GlossaryReaderService {

    @Cacheable(value = "glossaryList")
    public List<Glossary> readGlossary(String url) {

        XmlReader reader = null;
        List<Glossary> extractedGlossay = new ArrayList<Glossary>();
        SyndEntry entry;
        SyndContent desc;
        Glossary glossaryList = null;
        try {
            String decodedURL = URLDecoder.decode(url, "UTF-8");
            reader = new XmlReader(new URL(decodedURL));
            SyndFeed feed = new SyndFeedInput().build(reader);

            for (Iterator i = feed.getEntries().iterator(); i.hasNext();) {
                entry = (SyndEntry) i.next();
                desc = entry.getDescription();
                if (desc != null) { ...
                        extractedGlossay.add(glossaryList);
                    }
                }
            }

        } catch (IOException | IllegalArgumentException | FeedException e) {
            log.error("Error reading RSS feed", e);
        } finally {
            IOUtils.closeQuietly(reader);
        }

        return extractedGlossay;
    }

/webmi/src/main/resources/ehcache.xmlにもechahe.xmlがあります:

<?xml version="1.0" encoding="UTF-8"?>
   <ehcache>
  <defaultCache maxElementsInMemory="500" eternal="true"
    overflowToDisk="false" memoryStoreEvictionPolicy="LFU" />
  <cache name="info" maxElementsInMemory="500" eternal="true"
    overflowToDisk="false" memoryStoreEvictionPolicy="LFU" />
   </ehcache>

問題は、アプリケーションコンテンツにBeanを追加するときにエラーが発生することです。これがapplication-content.xmlのpartinです:

<!--  CACHE CONFIGURATION  -->
<cache:annotation-driven />
<bean id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cache-manager-ref="ehcache" />
    <bean id="ehcache class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:config location="classpath:ehcache.xml" />

しかし、それは私に次のエラーを与えます:

 2014-02-26 16:56:47 [ERROR] ContextLoader  - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cache.interceptor.CacheInterceptor#0': Cannot resolve reference to bean 'cacheManager' while setting bean property 'cacheManager'; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.cache.ehcache.EhCacheCacheManager] for bean with name 'cacheManager' defined in class path resource [application-context.xml]; nested exception is Java.lang.ClassNotFoundException: org.springframework.cache.ehcache.EhCacheCacheManager
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.Java:329)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.Java:107)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.Java:1391)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.Java:1132)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.Java:522)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.Java:461)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.Java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.Java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.Java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.Java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.Java:626)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.Java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.Java:479)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.Java:389)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.Java:294)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.Java:112)
    at org.Apache.catalina.core.StandardContext.listenerStart(StandardContext.Java:4939)
    at org.Apache.catalina.core.StandardContext.startInternal(StandardContext.Java:5434)
    at org.Apache.catalina.util.LifecycleBase.start(LifecycleBase.Java:150)
    at org.Apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.Java:1559)
    at org.Apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.Java:1549)
    at Java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.Java:334)
    at Java.util.concurrent.FutureTask.run(FutureTask.Java:166)
    at Java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.Java:1145)
    at Java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.Java:615)
    at Java.lang.Thread.run(Thread.Java:724)
Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.cache.ehcache.EhCacheCacheManager] for bean with name 'cacheManager' defined in class path resource [application-context.xml]; nested exception is Java.lang.ClassNotFoundException: org.springframework.cache.ehcache.EhCacheCacheManager
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.Java:1266)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.Java:438)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.Java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.Java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.Java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.Java:194)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.Java:323)
    ... 25 more
Caused by: Java.lang.ClassNotFoundException: org.springframework.cache.ehcache.EhCacheCacheManager
    at org.Apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.Java:1714)
    at org.Apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.Java:1559)
    at org.springframework.util.ClassUtils.forName(ClassUtils.Java:260)
    at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.Java:416)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.Java:1287)
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.Java:1258)
    ... 31 more
Feb 26, 2014 4:56:47 PM org.Apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cache.interceptor.CacheInterceptor#0': Cannot resolve reference to bean 'cacheManager' while setting bean property 'cacheManager'; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.cache.ehcache.EhCacheCacheManager] for bean with name 'cacheManager' defined in class path resource [application-context.xml]; nested exception is Java.lang.ClassNotFoundException: org.springframework.cache.ehcache.EhCacheCacheManager
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.Java:329)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.Java:107)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.Java:1391)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.Java:1132)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.Java:522)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.Java:461)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.Java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.Java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.Java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.Java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.Java:626)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.Java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.Java:479)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.Java:389)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.Java:294)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.Java:112)
    at org.Apache.catalina.core.StandardContext.listenerStart(StandardContext.Java:4939)
    at org.Apache.catalina.core.StandardContext.startInternal(StandardContext.Java:5434)
    at org.Apache.catalina.util.LifecycleBase.start(LifecycleBase.Java:150)
    at org.Apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.Java:1559)
    at org.Apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.Java:1549)
    at Java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.Java:334)
    at Java.util.concurrent.FutureTask.run(FutureTask.Java:166)
    at Java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.Java:1145)
    at Java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.Java:615)
    at Java.lang.Thread.run(Thread.Java:724)

私はこれに一日中取り組んでいますが、何が問題なのかわかりません。誰かが答えを知っているなら、助けてください!!!

編集: spring-contect-supportを追加しました。これがPomの依存関係です:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>4.0.1.RELEASE</version>
    </dependency>
    <!-- Ehcache -->
    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache</artifactId>
        <version>2.8.1</version>
    </dependency>

ただし、エラーは次のように変更されました。

 SEVERE: Servlet.service() for servlet [default-dispatcher] in context with path [/webmi] threw exception [Request processing failed; nested exception is Java.lang.IllegalArgumentException: Cannot find cache named [glossaryList] for CacheableOperation[public Java.util.List com.service.GlossaryReaderService.readGlossary(Java.lang.String)] caches=[glossaryList] | condition='' | key=''] with root cause
Java.lang.IllegalArgumentException: Cannot find cache named [glossaryList] for CacheableOperation[public Java.util.List com.service.GlossaryReaderService.readGlossary(Java.lang.String)] caches=[glossaryList] | condition='' | key=''
    at org.springframework.cache.interceptor.CacheAspectSupport.getCaches(CacheAspectSupport.Java:163)
    at org.springframework.cache.interceptor.CacheAspectSupport$CacheOperationContext.<init>(CacheAspectSupport.Java:443)
    at org.springframework.cache.interceptor.CacheAspectSupport.getOperationContext(CacheAspectSupport.Java:173)
    at org.springframework.cache.interceptor.CacheAspectSupport.createOperationContext(CacheAspectSupport.Java:404)
    at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.Java:192)
    at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.Java:66)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.Java:172)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.Java:631)
    at com.lloydsbanking.webmi.service.GlossaryReaderService$$EnhancerByCGLIB$$26769e11.readGlossary(<generated>)
    at com.webmi.web.RssController.getGlossory(RssController.Java:50)
    at Sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at Sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.Java:57)
    at Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:43)
15
user261002

このクラスはspring-context-supportオプションのモジュール。プロジェクトの依存関係として追加したことを確認してください。

<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context-support</artifactId>
   <version>X.Y.Z.RELEASE</version>
</dependency>
27
Stephane Nicoll

あなたのスプリングの設定は素晴らしいです、私はウェブ上の多くのタイプミスの例を見ることができます。

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache" />
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="/WEB-INF/ehcache.xml" />

備考:両方の依存関係が必要です:

<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>2.8.3</version>
</dependency>

<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context-support</artifactId>
   <version>3.2.8.RELEASE</version>
</dependency>

1つ目は実装、2つ目はスプリングラッパーです。両方を使用しない場合、実装クラスの検索に関連する例外が発生します。

9
Petar

Pom.xmlで以下のMaven依存関係を使用します

<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>4.3.4.RELEASE</version>
    </dependency>

また、バージョン4.3.4を使用していることを確認してください。 5.0.6およびその他の最新バージョンを使用しないでください。私にとってはうまくいきませんでした。

0
topcoder1306