Java 弹簧&x27;s可缓存不工作-不从缓存返回结果
我试图在一个方法上使用Spring@Cacheable,但它不起作用Java 弹簧&x27;s可缓存不工作-不从缓存返回结果,java,spring,caching,Java,Spring,Caching,我试图在一个方法上使用Spring@Cacheable,但它不起作用 import org.springframework.cache.annotation.Cacheable; public class CacheableService { @Cacheable(value = "entityCount") public int someEntityCount(final String criteria) { System.out.p
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;
} }
bean初始化:
<?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>
请帮助我。您正在测试中创建一个新的
CacheableService
实例,这意味着它不是由Spring管理的,没有任何Spring注释可以在其中工作
为了让spring生效,您需要将服务作为bean自动连接到测试类中您是否尝试将
@service
添加到CacheableService
并使用compant scan
自动连接服务实例?谢谢Nir。只是一个后续问题。在我们的服务中,我们已经初始化了一个Bean(比如“a”),并使用-new CacheableService()在内部构造了一个“CacheableService”对象。正如您提到的,我们不能在类的方法上使用spring缓存,使用“new”关键字初始化。那么,缓存方法响应的替代方法或方法是什么呢?如果bean a使用“new”关键字创建服务,那么可缓存的注释在那里将不起作用,因为服务将不是Springbean。另一种选择是——服务应该作为一个bean创建,并自动连接到使用itThanks Nir的所有其他bean中。如果由于硬耦合/大服务而无法实现。然后我们将不得不转向其他缓存解决方案,如Guava缓存或类似的解决方案。对吗?对我来说,听起来硬耦合是用新的,autowire是我心目中的松耦合方式,但对于你的问题——如果它不是一个bean——它不会工作。非常感谢你。
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");
}
}