简体   繁体   English

Spring 的 Cacheable 不起作用 - 不从 Cache 返回结果

[英]Spring's Cacheable not working - Not returning results from Cache

I'm trying to use Spring @Cacheable on a Method but its Not Working.我正在尝试在方法上使用 Spring @Cacheable 但它不起作用。

 import org.springframework.cache.annotation.Cacheable;

public class CacheableService {


    @Cacheable(value = "entityCount")
    public int someEntityCount(final String criteria) {
        System.out.println("Inside function : " + criteria);
        return 5;
    } }

Beans Initialization :豆类初始化:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        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
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

<cache:annotation-driven />
<context:annotation-config/>
<aop:aspectj-autoproxy />

<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
    <property name="caches">
        <set>
            <bean name = "impactLevelsCache" class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
                <property name="name" value="impactLevels" />
            </bean>
            <bean name = "entityCountBean" class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
                <property name="name" value="entityCount" />
            </bean>
        </set>
</bean>


</beans>

I am not Getting what mistake I'm making.我不明白我犯了什么错误。 It always invoking the method.它总是调用方法。 I have also enabled Trace Logs but its not logging anything specific to Caching.我还启用了跟踪日志,但它没有记录任何特定于缓存的内容。 Also tried calling it from test cases, but cache not working.还尝试从测试用例中调用它,但缓存不起作用。 (Invocation is from different class. There is no Self-Invocation). (调用来自不同的类。没有自调用)。

import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
// Using same Bean configuration for Testing also.
@ContextConfiguration(locations = "/com/amazon/pickuppointcapacity/test/appconfig2.xml")
public class RoughTest {


    @Test
    public void testSomeEntityCountCalledTwice_shouldCallDaoMethodOnce() {
        CacheableService cacheableService = new CacheableService();
        cacheableService.someEntityCount("A");
        cacheableService.someEntityCount("A");
        cacheableService.someEntityCount("A");
        cacheableService.someEntityCount("A");

    }
}

Please help me out.请帮帮我。

You are creating a new instance of CacheableService in your test, which means it is not managed by Spring and non of the Spring annotations will work in it.您正在测试中创建CacheableService的新实例,这意味着它不受 Spring 管理,并且 Spring 注释中的任何一个都不会在其中工作。

For spring to kick in, you need to get your service as a bean, autowired into your test class为了春天开始,您需要将您的服务作为 bean,自动装配到您的测试类中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM