2016-11-13 37 views
1

我有一個調用webservice的方法和相同的輸入參數,我想要的結果被緩存。所以,這裏是我做了什麼至今: 主類:得到java.lang.ArrayStoreException上Springboot和的Ehcache

import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.boot.context.web.SpringBootServletInitializer; 
import org.springframework.cache.CacheManager; 
import org.springframework.cache.annotation.EnableCaching; 
import org.springframework.cache.ehcache.EhCacheCacheManager; 
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.core.io.ClassPathResource; 

@EnableCaching 
@ComponentScan 
@SpringBootApplication 
public class AccServer extends SpringBootServletInitializer { 

    @Bean 
    public CacheManager cacheManager() { 
     return new EhCacheCacheManager(ehCacheCacheManager().getObject()); 
    } 

    @Bean 
    public EhCacheManagerFactoryBean ehCacheCacheManager() { 
     EhCacheManagerFactoryBean cmfb = new EhCacheManagerFactoryBean(); 
     cmfb.setConfigLocation(new ClassPathResource("ehcache.xml")); 
     cmfb.setShared(true); 
     return cmfb; 
    } 

} 

方法緩存:

import java.util.List; 

import org.springframework.cache.annotation.Cacheable; 
import org.springframework.stereotype.Service; 

@Service 
public class SomeClass implements ISomeClass { 

    @Override 
    @Cacheable("acc") 
    public List<Integer> trs() { 
     return webSrv.trs(); 
    } 

} 

ehcache.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false"> 
    <diskStore path="java.io.tmpdir" /> 
    <cache name="acc" maxElementsInMemory="100" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> 
</ehcache> 

我也得到:

Caused by: java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy 
    at sun.reflect.annotation.AnnotationParser.parseClassArray(Unknown Source) 
    at sun.reflect.annotation.AnnotationParser.parseArray(Unknown Source) 
    at sun.reflect.annotation.AnnotationParser.parseMemberValue(Unknown Source) 
    at sun.reflect.annotation.AnnotationParser.parseAnnotation2(Unknown Source) 
    at sun.reflect.annotation.AnnotationParser.parseAnnotations2(Unknown Source) 
    at sun.reflect.annotation.AnnotationParser.parseAnnotations(Unknown Source) 
    at java.lang.Class.createAnnotationData(Unknown Source) 
    at java.lang.Class.annotationData(Unknown Source) 
    at java.lang.Class.createAnnotationData(Unknown Source) 
    at java.lang.Class.annotationData(Unknown Source) 
    at java.lang.Class.getAnnotation(Unknown Source) 
    at org.springframework.cache.annotation.SpringCacheAnnotationParser.getAnnotations(SpringCacheAnnotationParser.java:201) 
    at org.springframework.cache.annotation.SpringCacheAnnotationParser.parseCacheAnnotations(SpringCacheAnnotationParser.java:64) 
    at org.springframework.cache.annotation.SpringCacheAnnotationParser.parseCacheAnnotations(SpringCacheAnnotationParser.java:52) 
    at org.springframework.cache.annotation.AnnotationCacheOperationSource$1.getCacheOperations(AnnotationCacheOperationSource.java:113) 
    at org.springframework.cache.annotation.AnnotationCacheOperationSource.determineCacheOperations(AnnotationCacheOperationSource.java:142) 
    at org.springframework.cache.annotation.AnnotationCacheOperationSource.findCacheOperations(AnnotationCacheOperationSource.java:110) 
    at org.springframework.cache.interceptor.AbstractFallbackCacheOperationSource.computeCacheOperations(AbstractFallbackCacheOperationSource.java:142) 
    at org.springframework.cache.interceptor.AbstractFallbackCacheOperationSource.getCacheOperations(AbstractFallbackCacheOperationSource.java:97) 
    at org.springframework.cache.interceptor.CacheOperationSourcePointcut.matches(CacheOperationSourcePointcut.java:39) 
    at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:211) 
    at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:248) 
    at org.springframework.aop.support.AopUtils.findAdvisorsThatCanApply(AopUtils.java:280) 
    at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findAdvisorsThatCanApply(AbstractAdvisorAutoProxyCreator.java:118) 
    at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findEligibleAdvisors(AbstractAdvisorAutoProxyCreator.java:88) 
    at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean(AbstractAdvisorAutoProxyCreator.java:69) 
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:346) 
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:298) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:422) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) 
    ... 135 more 

我也用@Cacheable("acc")應該針對結果緩存的方法的註釋。 任何答案將不勝感激。

+0

添加的代碼,你已獲得@Cacheable(「acc」)和@CachePut – developer

+0

@javaguy添加。 – Khodabakhsh

+0

我需要@Cacheput全班以及或與緩存該類任何其他操作,最好加全班,它會很容易找到 – developer

回答

1

的問題是與@Cacheabletrs()方法(您@ServiceSomeClass類的),因爲其中的你得到sun.reflect.annotation.TypeNotPresentExceptionProxy例外,它沒有返回List<Integer>對象。

您需要確保trs()被返回List<Integer>對象。

+0

我不這麼認爲,導致將return子句更改爲「返回新的ArrayList ()」仍然給出相同的異常。 – Khodabakhsh

+0

而且我在應用程序啓動時遇到這個異常! – Khodabakhsh

+0

您可以添加webSrv.trs()方法的代碼,以便我可以建議用於Service方法的返回類型以避免此異常? – developer

0

這個例外意味着什麼嘗試做一個註解,這不是出現在類路徑中查找。不幸的是,JDK並沒有多告訴我們,但我認爲我們已經改進了最近版本框架中的錯誤信息(並因此引導)。

你也可以在故障代碼添加斷點,它會告訴你它試圖解決的問題。這應該給你提示缺少註釋是什麼。大多數情況下,這與查找的組件並不嚴格相關(即它可能與緩存無關)。如果您分享示例應用程序,我們可以輕鬆搞清楚。

1

感謝傢伙怎麼在這個問題上做出了貢獻,最後調試的很長一段時間後,我才發現,有必要排除從項目的兩個依賴,使之成爲:

  <exclusion> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-actuator</artifactId> 
      </exclusion> 
      <exclusion> 
       <groupId>org.apache.tomcat.embed</groupId> 
       <artifactId>tomcat-embed-jasper</artifactId> 
      </exclusion> 
      <exclusion> 
       <groupId>javax.servlet</groupId> 
       <artifactId>jstl</artifactId> 
      </exclusion>