Java @可缓存的[Spring MVC]不工作
我的spring项目有一个jar包。我使用的是Spring 4.3.2 在我的资源文件夹中。我有一个基于xml的配置文件:abc-servlet-context.xmlJava @可缓存的[Spring MVC]不工作,java,spring,spring-mvc,spring-annotations,spring-cache,Java,Spring,Spring Mvc,Spring Annotations,Spring Cache,我的spring项目有一个jar包。我使用的是Spring 4.3.2 在我的资源文件夹中。我有一个基于xml的配置文件:abc-servlet-context.xml <?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSche
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<cache:annotation-driven />
<context:component-scan base-package="sayak.auth,
sayak.member"/>
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
p:name="memberInLastMonth"/>
</set>
</property>
</bean>
我在dao方法上使用@Cacheable,如下所示,并用@Component注释该类:
@Cacheable(value="memberInLastMonth" , key="#authenticationId")
public List<AuthenticationAuditTrail> findByAuthenticationId(long authenticationId) {
//DB call to fetch data
}
@Cacheable(value=“memberInLastMonth”,key=“#authenticationId”)
公共列表findByAuthenticationId(长身份验证ID){
//数据库调用以获取数据
}
当在同一个authenticationId上调用时,这个方法的结果不会被缓存,我使用了一个简单的断言来检查它,但没有得到所需的结果。任何线索都会有帮助
试验方法如下:
@Test
public void testMemberInLastMonth() {
Member member=getMember();
List<AuthenticationAuditTrail> records = getDAO()
.findByAuthenticationId(member.getAuthentication().getId(),
DateUtil.getPastMonth(1));
List<AuthenticationAuditTrail> cachedRecords = getDAO()
.findByAuthenticationId(member.getAuthentication().getId(),
DateUtil.getPastMonth(1));
for(int i=0;i<records.size();i++){
AssertUtil.assertTrue(records.get(0) == cachedRecords.get(0));
}
@测试
public void testMemberInLastMonth()的测试成员{
Member=getMember();
列表记录=getDAO()
.findByAuthenticationId(成员.getAuthentication().getId(),
DateUtil.getPastMonth(1));
列表cachedRecords=getDAO()
.findByAuthenticationId(成员.getAuthentication().getId(),
DateUtil.getPastMonth(1));
对于(int i=0;i)您如何在DAO中调用该方法,您是否有一个测试用例可以发布在此处供参考。您是在谈论“FindByaAuthenticationId”方法调用还是方法内部的逻辑?探索@CachedResult.
@ShanuGupta您可以共享一些链接吗?@HimanshuBhardwaj“FindByaAuthenticationId”是从另一个类调用的,在那里我实际上在列表元素中循环,并执行一个简单的asertEquals