Spring的Cacheable不起作用- 未从缓存返回结果

huangapple go评论80阅读模式
英文:

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

问题

我正在尝试在方法上使用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初始化:

<?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>
    </property>
</bean>

</beans>

我不明白我在犯什么错误。它总是调用该方法。我也启用了Trace日志,但没有记录任何与缓存有关的特定信息。
我还尝试从测试用例中调用它,但缓存不起作用。(调用来自不同的类。没有自调用。)

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)
// 使用相同的Bean配置进行测试。
@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");
    }
}

请帮助我解决这个问题。

英文:

I'm trying to use Spring @Cacheable on a Method but its Not Working.

 import org.springframework.cache.annotation.Cacheable;

public class CacheableService {


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

Beans Initialization :

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

&lt;cache:annotation-driven /&gt;
&lt;context:annotation-config/&gt;
&lt;aop:aspectj-autoproxy /&gt;

&lt;bean id=&quot;cacheManager&quot; class=&quot;org.springframework.cache.support.SimpleCacheManager&quot;&gt;
    &lt;property name=&quot;caches&quot;&gt;
        &lt;set&gt;
            &lt;bean name = &quot;impactLevelsCache&quot; class=&quot;org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean&quot;&gt;
                &lt;property name=&quot;name&quot; value=&quot;impactLevels&quot; /&gt;
            &lt;/bean&gt;
            &lt;bean name = &quot;entityCountBean&quot; class=&quot;org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean&quot;&gt;
                &lt;property name=&quot;name&quot; value=&quot;entityCount&quot; /&gt;
            &lt;/bean&gt;
        &lt;/set&gt;
&lt;/bean&gt;


&lt;/beans&gt;

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 = &quot;/com/amazon/pickuppointcapacity/test/appconfig2.xml&quot;)
public class RoughTest {


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

    }
}

Please help me out.

答案1

得分: 2

你正在测试中创建一个CacheableService的新实例,这意味着它不受Spring管理,其中的任何Spring注解都不起作用。

要让Spring生效,您需要将您的服务作为一个bean获取,并在测试类中进行自动注入。

英文:

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.

For spring to kick in, you need to get your service as a bean, autowired into your test class

huangapple
  • 本文由 发表于 2020年9月15日 14:20:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63896205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定