2015-10-14 26 views
0

我想找到一种在ehCache装饰器类中使用Spring依赖关系注入的好方法。 我有我用下面的缓存配置ehcache.xml中:如何在ehCache.xml中声明的ehCache装饰器中传递依赖关系

<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 context我实现了ApplicationContextAware接口。问题是在createDecoratedEhcache()之后调用setApplicationContext()方法,并且当MyUpdatingCacheEntryFactory被实例化时不能设置依赖关系。

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

回答

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

它为我工作。如果有人有更好的解决方案,请告诉我。

相关问题