gpt4 book ai didi

java - 如何传递 ehcache.xml 中声明的 ehCache 装饰器中的依赖项

转载 作者:行者123 更新时间:2023-11-30 07:52:51 25 4
gpt4 key购买 nike

我想找到一种在 ehCache 装饰器类中使用 Spring 依赖注入(inject)的好方法。我的 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直接注入(inject)MyDependency,因为装饰器是通过<cacheDecoratorFactory/>实例化的我的 ehcache.xml 中的标记。

为了能够使用 spring 上下文,我实现了 ApplicationContextAware界面。问题是setApplicationContext()方法在 createDecoratedEhcache() 之后调用,并且当 MyUpdatingCacheEntryFactory 时无法设置依赖项已实例化。

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

最佳答案

好的,我可以通过以下方式通过 Spring 完成此操作:

我已删除 <cacheDecoratorFactory/> ehcache.xml 中的标签并添加了一个缓存配置 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);
}
}

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

关于java - 如何传递 ehcache.xml 中声明的 ehCache 装饰器中的依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33132642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com