Java 未截获用@Cacheable注释的方法
我不熟悉Spring缓存。我在maven pom中使用spring-boot-starter-1.4.0.RELEASE。据我所知,如果我采取以下措施:Java 未截获用@Cacheable注释的方法,java,spring,spring-cache,Java,Spring,Spring Cache,我不熟悉Spring缓存。我在maven pom中使用spring-boot-starter-1.4.0.RELEASE。据我所知,如果我采取以下措施: @Configuration @EnableCaching public class TestApplication { @Bean public CacheManager cacheManager() { // configure and return an implementation of Spring's
@Configuration
@EnableCaching
public class TestApplication {
@Bean
public CacheManager cacheManager() {
// configure and return an implementation of Spring's CacheManager SPI
SimpleCacheManager cacheManager = new SimpleCacheManager();
cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("default")));
return cacheManager;
}
@Bean
public MyService myService() {
// configure and return a class having @Cacheable methods
return new MyService();
}
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(TestConfiguration.class);
MyService ms = ctx.getBean(MyService.class);
ms.doCacheableOperation(); // calls the underlying method
ms.doCacheableOperation(); // SHOULD just consult the cache
}
}
public class MyService {
@Cacheable
public String doCacheableOperation() {
System.out.println("======================CALLING EXPENSIVE METHOD=======================");
return "done";
}
}
有这样一节课:
@Configuration
@EnableCaching
public class TestApplication {
@Bean
public CacheManager cacheManager() {
// configure and return an implementation of Spring's CacheManager SPI
SimpleCacheManager cacheManager = new SimpleCacheManager();
cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("default")));
return cacheManager;
}
@Bean
public MyService myService() {
// configure and return a class having @Cacheable methods
return new MyService();
}
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(TestConfiguration.class);
MyService ms = ctx.getBean(MyService.class);
ms.doCacheableOperation(); // calls the underlying method
ms.doCacheableOperation(); // SHOULD just consult the cache
}
}
public class MyService {
@Cacheable
public String doCacheableOperation() {
System.out.println("======================CALLING EXPENSIVE METHOD=======================");
return "done";
}
}
当main方法在TestApplication中运行时,对MyServce#doCacheableOperation的第一个调用应该输出到屏幕,但第二个不应该输出,因为结果将从第一次缓存。然而,事实并非如此;输出显示两次
配置代码从Javadoc中提取,用于启用缓存:
让我困惑的一件事是,当我调试和检查MyService实例时,它只是原始对象,而不是包装在任何CGLib子类中,等等
我需要如何更改配置/方法才能缓存MyService#doCacheableOperation的结果?噢,天哪。找到了。我发送给SpringApplication#run的类中有一个简单的输入错误: 应该是
SpringApplication.run(TestApplication.class)
现在一切似乎都井然有序了