coderAJ
coderAJ

Reputation: 330

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

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 = "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.

Upvotes: 1

Views: 298

Answers (1)

Nir Levy
Nir Levy

Reputation: 12953

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

Upvotes: 2

Related Questions