web-dev-qa-db-fra.com

configuration ehcache dans le framework Spring

J'essaie de charger du contexte à partir d'un flux RSS et de le transmettre en cache au client à l'aide de la bibliothèque ehcache au printemps. voici mon code:

    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;
    }

également sous /webmi/src/main/resources/ehcache.xml j'ai le 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>

mais le problème est que lorsque je veux ajouter les beans dans le contenu de l'application, cela donne une erreur. voici le partin application-content.xml:

<!--  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" />

mais cela me donne l'erreur suivante:

 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)

J'y ai travaillé toute la journée, mais je ne sais pas quel est le problème. s'il vous plaît si quelqu'un connaît la réponse, aidez !!!

Edit: J'ai ajouté le support de ressort-contect, voici les dépendances dans le 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>

mais l'erreur a été modifiée comme suit:

 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

Cette classe est située dans le spring-context-support module facultatif. Veuillez vous assurer que vous l'avez ajouté en tant que dépendance de votre projet.

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

Votre configuration de ressort est excellente, je vois des exemples avec beaucoup de fautes de frappe sur le web.

<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" />

Remarque: vous avez besoin des deux dépendances:

<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>

le premier est la mise en œuvre, le second est une enveloppe à ressort autour. Si vous n'utilisez pas les deux, vous aurez des exceptions liées à la recherche de la classe d'implémentation.

9
Petar

Utilisez la dépendance maven ci-dessous dans votre pom.xml

<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>

Assurez-vous également que vous utilisez la version 4.3.4. N'utilisez pas 5.0.6 et une autre dernière version, cela n'a pas fonctionné pour moi.

0
topcoder1306