Reputation: 7752
I am new to Spring caching. I am using spring-boot-starter-1.4.0.RELEASE in my maven pom. As far as I understand the documentation, if I take something like this:
@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
}
}
And have a class like this:
public class MyService {
@Cacheable
public String doCacheableOperation() {
System.out.println("======================CALLING EXPENSIVE METHOD=======================");
return "done";
}
}
When the main method runs in TestApplication, the first call to MyServce#doCacheableOperation should output to the screen, but the second should not since the result would be cached from the first time. This, however, is not the case; the output shows twice.
The configuration code is lifted from the Javadoc for EnableCaching: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/cache/annotation/EnableCaching.html
One thing that does puzzle me is that when I debug and inspect the instance of MyService, it is just the raw object, not wrapped in any CGLib subclass, etc.
How do I need to change my configuration/approach so that the result of MyService#doCacheableOperation is cached?
Upvotes: 1
Views: 507
Reputation: 7752
Oh, boy. Found it. There was a simple typo in the class I was sending to SpringApplication#run:
SpringApplication.run(TestConfiguration.class)
should have been
SpringApplication.run(TestApplication.class)
Everything seems to be in order now!
Upvotes: 2