I am trying to setup a bootstrapcacheloader which will query the database and populate the cache. Here I am using ehcache integrated with spring. But the problem is that I am not able to get the dependencies wired into my cacheloader implementation. The @Autowired,@Resource,@Configurable none of them seem to work. Quite obviously the cacheloader instantiation is not done by Spring container , but is there a way I can inject a spring created cacheloader instance into the cachemanager and bootstrap it? My implementation details below. ehcache.xml
<cache name="MyCache"
maxElementsInMemory="100000"
eternal="false"
overflowToDisk="false"
timeToLiveSeconds="500">
<!-- <pinning store="localMemory"/> -->
<bootstrapCacheLoaderFactory class="net.tristargroup.claims.helper.ClaimsCacheLoaderFactory" properties="bootstrapAsynchronously=true"/>
<cacheEventListenerFactory class="net.tristargroup.claims.helper.TristarCacheEventListenerFactory" listenFor="all"/>
</cache>
Spring Context xml
<cache:annotation-driven/>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager"><ref local="ehcache"/></property>
</bean>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" depends-on="cacheLoader">
<property name="configLocation" value="classpath:ehcache.xml"/>
</bean>
Cache loader class
@Configurable
public class ClaimsCacheLoaderFactory extends BootstrapCacheLoaderFactory{
@Resource
CacheManager cacheManager;
.
.
.
@Override
public BootstrapCacheLoader createBootstrapCacheLoader(Properties arg0) {
System.out.println("Create cache loader method . Cache manager is ->"+cacheManager);
BootstrapCacheLoader cacheLoader = new ClaimsCacheLoader();
return cacheLoader;
}
The cacheManager instance is always null here even if I specify it as an Autowired attribute. The problem is present even in the cache event listeners. Someone please help me on this.
If the cacheManager is null, Tthat means you haven't made the bean available to your package. Add a @ComponentScan("yourpackage") in the same place you defined your bean and add @Component where you want to use your cacheManager. This is old, but i hope it helps someone else.