我有一个调用web服务的方法,对于相同的输入参数,我希望缓存结果。所以,这是我到目前为止所做的: 主要课程:
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 :(得分:1)
问题在于@Cacheable
trs()
方法(您的@Service
SomeClass
类)没有返回List<Integer>
对象因为您正在获取sun.reflect.annotation.TypeNotPresentExceptionProxy
例外。
您需要确保trs()
正在返回List<Integer>
对象。
答案 1 :(得分: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>
答案 2 :(得分:0)
该异常意味着尝试对类路径中不存在的注释进行查找。不幸的是,JDK并没有告诉我们太多关于它的信息,但我认为我们已经在最近版本的框架中改进了错误信息(因此启动了)。
您还可以在错误代码中添加断点,它会告诉您它正在尝试解决的问题。那应该会给你一个缺失注释的提示。大多数情况下,这与查找的组件并不严格相关(即它可能与缓存无关)。如果您共享示例应用程序,我们可以轻松搞清楚。