I want to use read-through and write-through cache strategies using Ehcache and I am using XML configuration to configure Ehcache, I want to set spring bean (CacheLoaderWriter implementation) to cache configuration, I can't do that using XML configuration, because then Ehcache instantiate the bean using default constructor and spring DI will not work, so, How Can I override/set spring managed CacheLoaderWriter bean to cache defined in XML config file in java-config?
I tried setting CacheLoaderWriter class in my XML file as below
<cache alias="employeeEntityCache">
<key-type>java.lang.Long</key-type>
<value-type>com.example.spring.cahce.model.Employee</value-type>
<loader-writer>
<class>com.example.spring.cahce.stragegy.readwritethrough.EmployeeEntityLoaderWriter</class>
</loader-writer>
<resources>
<heap unit="entries">100</heap>
</resources>
</cache>
But, then Ehcache instantiate LoaderWriter bean and hence spring DI will not work
My java config, to load cache configuration from xml
@Bean
@Qualifier("jcachexml")
public javax.cache.CacheManager jCacheCacheManager() {
CachingProvider cachingProvider = Caching.getCachingProvider();
try {
javax.cache.CacheManager manager = cachingProvider.getCacheManager(
getClass().getResource("/ehcache-jsr107-config.xml").toURI(),
getClass().getClassLoader());
return manager;
} catch (URISyntaxException e) {
e.printStackTrace();
}
return null;
}
I want a way to override cache configuration set in Ehcache XML configuration, so that I can set spring managed CacheLoaderWriter bean to cache configuration and can inject dependencies using DI
Ehcache 3.8.0 has added new feature Configuration Derivation, using which we can use to override the configurations,
refer Configuration Derivation
So, I was able to override by XML configuration in java config and also set CacheLoaderWriter as a fully initialized spring bean.