如何在ehcache.xml

时间:2015-10-14 18:12:48

标签: java spring dependency-injection ehcache

我想找到一种在ehCache装饰器类中使用Spring依赖注入的好方法。 我的ehcache.xml包含以下缓存配置:

<cache name="MY_CACHE"
       memoryStoreEvictionPolicy="LRU">
    <cacheDecoratorFactory class="org.company.MyCacheDecoratorFactory"/>
</cache>

我有以下装饰器实现:

public class MyCacheDecoratorFactory extends CacheDecoratorFactory implements ApplicationContextAware {

    private MyDependency myDependency;

    @Override
    public Ehcache createDecoratedEhcache(final Ehcache ehcache, final Properties properties) {
        final UpdatingSelfPopulatingCache selfPopulatingCache = new UpdatingSelfPopulatingCache(ehcache,
                new MyUpdatingCacheEntryFactory());
        selfPopulatingCache.setTimeoutMillis(30000);

        return selfPopulatingCache;
    }

    @Override
    public Ehcache createDefaultDecoratedEhcache(final Ehcache ehcache, final Properties properties) {
        return this.createDecoratedEhcache(ehcache, properties);
    }

    @Override
    public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {
        myDependency = applicationContext.getBean(MyDependency.class);
    }

    private class MyUpdatingCacheEntryFactory implements UpdatingCacheEntryFactory {
        @Override
        public void updateEntryValue(final Object key, final Object value) throws Exception {
            myDependency.update(key, value);
        }

        @Override
        public Object createEntry(final Object key) throws Exception {
            return myDependency.create(key);
        }
    }
}

所以,我不能直接使用@Autowire注入MyDependency,因为装饰器是通过我的ehcache.xml中的<cacheDecoratorFactory/>标签实例化的。

为了能够使用spring上下文我实现了ApplicationContextAware接口。问题是在setApplicationContext()之后调用createDecoratedEhcache()方法,并且在MyUpdatingCacheEntryFactory实例化时不能设置依赖关系。

我应该如何正确地将我的spring依赖项传递给装饰器?

1 个答案:

答案 0 :(得分:0)

好的,我能够通过以下方式通过Spring来实现它:

我从ehcache.xml中删除了<cacheDecoratorFactory/>标记,并添加了一个缓存配置bean,它使用缓存管理器在初始化时用装饰器替换缓存:

@Component
public class CacheInitConfigurer implements InitializingBean {
    @Autowired
    private CacheManager cacheManager;
    @Autowired
    private MyCacheDecoratorFactory decoratorFactory;

    @Override
    public void afterPropertiesSet() throws Exception {
        final Ehcache myCache = cacheManager.getEhcache("MY_CACHE");
        cacheManager.replaceCacheWithDecoratedCache(myCache,
            decoratorFactory.createDefaultDecoratedEhcache(myCache, null));
    }
}

我已按如下方式更改了MyCacheDecoratorFactory:

@Component
public class MyCacheDecoratorFactory extends CacheDecoratorFactory {
    @Autowired
    private MyUpdatingCacheEntryFactory myUpdatingCacheEntryFactory;

    @Override
    public Ehcache createDecoratedEhcache(final Ehcache ehcache, final Properties properties) {
        final SelfPopulatingCache selfPopulatingCache = new UpdatingSelfPopulatingCache(ehcache,
                myUpdatingCacheEntryFactory);
        selfPopulatingCache.setTimeoutMillis(30 * 1000);

        return selfPopulatingCache;
    }

    @Override
    public Ehcache createDefaultDecoratedEhcache(final Ehcache ehcache, final Properties properties) {
        return this.createDecoratedEhcache(ehcache, properties);
    }
}

它对我有用。如果有人有更好的解决方案,请告诉我。