Reputation: 16844
I have this java based JPA configuration for my spring project:
@Configuration
@EnableJpaRepositories(basePackageClasses = {PackageMarker.class})
@EnableTransactionManagement(proxyTargetClass = true)
@EnableCaching
public class FooJPAConfig implements CachingConfigurer {
@Bean
@Override
public CacheManager cacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("default")));
return cacheManager;
}
@Bean
@Override
public KeyGenerator keyGenerator() {
return new DefaultKeyGenerator();
}
//...
}
How can I tell spring to use a specific ehcache.xml
file?
Upvotes: 1
Views: 3324
Reputation: 64039
You need to alter cacheManager
in order to integrate EhCache. Your current code does not make EhCache enter the picture.
The configuration would look like
@Configuration
@EnableJpaRepositories(basePackageClasses = {PackageMarker.class})
@EnableTransactionManagement(proxyTargetClass = true)
@EnableCaching
public class FooJPAConfig implements CachingConfigurer {
@Bean
public EhCacheManagerFactoryBean cacheFactoryBean() {
EhCacheManagerFactoryBean ehCacheManagerFactoryBean = new EhCacheManagerFactoryBean();
ehCacheManagerFactoryBean.setConfigLocation(new ClassPathResource("whatever-name.xml")); //this is where you set the location of the eh-cache configuration file
return ehCacheManagerFactoryBean;
}
@Bean
@Override
public CacheManager cacheManager() {
EhCacheCacheManager cacheManager = new EhCacheCacheManager();
cacheManager.setCacheManager(cacheFactoryBean().getObject());
return cacheManager;
}
@Bean
@Override
public KeyGenerator keyGenerator() {
return new DefaultKeyGenerator();
}
}
You will also have to have spring-context-support
as a dependency on your classpath (applies for Spring 3.2)
Note that the code above activates Spring -EhCache integration, not JPA - EhCache integration. That means that you can use Spring's @Cacheable
not EhCache's @Cache
on entities.
Upvotes: 2
Reputation: 16844
After all I could solve the problem by adding this code to the configuration class:
protected static final String EHCACHE_CONFIGURATIONRESOURCENAME_PROPERTY = "net.sf.ehcache.configurationResourceName";
@Bean(name = BEAN_ENTITY_MANAGER_FACTORY)
public EntityManagerFactory entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = createLocalContainerEntityManagerFactoryBean();
// ...
processOptionalProperty(EHCACHE_CONFIGURATIONRESOURCENAME_PROPERTY, em);
return em.getObject();
}
protected void processOptionalProperty(String propertyName, LocalContainerEntityManagerFactoryBean em) {
String value = "";// read propertyName from configuration file
setPropertyValue(propertyName, em, value);
}
protected void setPropertyValue(String propertyName, LocalContainerEntityManagerFactoryBean em, Object value) {
if (value != null) {
Map<String, Object> jpaPropertyMap = em.getJpaPropertyMap();
jpaPropertyMap.put(propertyName, value);
em.setJpaPropertyMap(jpaPropertyMap);
}
}
Upvotes: 0