2015-10-14 45 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); 
    } 
} 

它爲我工作。如果有人有更好的解決方案,請告訴我。