简体   繁体   中英

How to set location of ehcache.xml in spring java based configuration?

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?

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.

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM