1

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?

BetaRide
  • 16,207
  • 29
  • 99
  • 177

2 Answers2

2

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.

geoand
  • 60,071
  • 24
  • 172
  • 190
  • Cannot get this working. What does your last note mean? I'm with Sprint 3.2.9 there's no `EhCacheManagerFactoryBean` class. – BetaRide Sep 02 '14 at 12:58
  • Thanks for taking your time. Unfortunately adding `spring-context-support` does not help. I cannot find `EhCacheManagerFactoryBean`. – BetaRide Sep 02 '14 at 14:32
  • @BetaRide No problem! The class is definitely in `spring-context-support` in Spring 3.2. Check out http://stackoverflow.com/a/13956607/2504224. Are you sure all your dependencies are Spring 3.2 and you don't have any older ones? – geoand Sep 02 '14 at 14:43
0

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);
    }
}
BetaRide
  • 16,207
  • 29
  • 99
  • 177