Pregunta

Looking at the Ehcahce implementation of net.sf.cache.JS107, I am trying to achieve the following code snippet to produce the JCache implementation as a Spring managed bean..

  Ehcache Ehcache = new net.sf.ehcache.Cache(...);
  net.sf.jsr107cache.Cache cache = new JCache(ehcache);
  manager.addJCache(cache);

Here is my manager bean:

   <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="shared" value="true"/>
    </bean>

Here is my Ehcache bean:

<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
        <property name="cacheManager" ref="cacheManager"/>
        <property name="diskPersistent" value="false"/>
</bean>

Here is how I create my JCache bean:

<bean id="jcache" class="net.sf.jsr107cache.Cache">
        <constructor-arg ref="ehcache"/>
</bean>

What I want is to be able to invoke the following method in Spring to apply my JCache to the Ehcache manager:

manager.addJCache(cache);

There must be a way to do this in Spring?

Thanks

¿Fue útil?

Solución

Don't bother with XML, just use Java @Configuration:

@Bean
public Ehcache ehcache() {
    return new net.sf.ehcache.Cache(...);
}

@Bean
public net.sf.jsr107cache.Cache jsrCache() {
    net.sf.jsr107cache.Cache cache = new JCache(ehcache());
    manager.addJCache(cache);
    return cache;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top