Spring Caching with EhCache
Alternatively, you can use Ehcache and use the Ehcache specific
CacheManager
as cache provider.Enable Caching
Add
@EnableCaching(mode=AdviceMode.ASPECTJ)
annotation on the configuration class if you are using Java based configuration.@Configuration ... @EnableCaching(mode=AdviceMode.ASPECTJ) public class JpaConfig {...}
Then, specify a
CacheManager
provider in your configuration. There is a EhCacheCacheManager
provided by Spring.@Bean public CacheManager cacheManager() { return new EhCacheCacheManager(ehcache().getObject()); } @Bean public EhCacheManagerFactoryBean ehcache() { EhCacheManagerFactoryBean ehcache = new EhCacheManagerFactoryBean(); //ehcache.setConfigLocation(new ClassPathResource("ehcache.xml")); return ehcache; }
Do not forget to add the ehcache dependency in your Maven pom.xml file.
<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> </dependency>
Code sample
Check out the sample codes.
And explore the spring-cache-ehcache folder.
评论