Skip to content

Instantly share code, notes, and snippets.

@andresteingress
Created April 23, 2014 18:22
Show Gist options
  • Save andresteingress/11226958 to your computer and use it in GitHub Desktop.
Save andresteingress/11226958 to your computer and use it in GitHub Desktop.
ConditionalOnExpression - A conditional EhCache manager configuration
@Configuration
@EnableCaching
@ConditionalOnExpression("${spring.cacheable.cache}")
public class EhCacheConfiguration {
@Autowired
private net.sf.ehcache.CacheManager cacheManager;
@Bean(name = "cacheManager")
public CacheManager ehCacheCacheManager()
{
return new EhCacheCacheManager(cacheManager);
}
@Bean
public EhCacheManagerFactoryBean ehcache() {
EhCacheManagerFactoryBean ehCacheManagerFactoryBean = new EhCacheManagerFactoryBean();
ehCacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
// the cache manager should only be created once, so this must be shared == true
ehCacheManagerFactoryBean.setShared(true);
return ehCacheManagerFactoryBean;
}
}
@andresteingress
Copy link
Author

the cache manager creation can therefore be controlled by the application property spring.cacheable.cache.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment