使用Spring智能地获取EhCache实例

9

我需要按名称获取特定的EhCache实例,如果可能的话,我希望通过自动装配来完成。考虑到以下已自动配置的控制器,我如何自动装配所需的缓存实例?

@Controller 
public class MyUniqueService {
    ...
}

<beans ...>
    <ctx:component-scan base-package="my.controllers"/>
    <mvc:annotation-driven />
</beans>

我该如何在应用程序上下文中配置EhCache?我没有看到任何来自EhCache的日志消息,说明它正在加载位于/WEB-INF/目录中的ehcache.xml文件。我该如何使其加载?
我如何将EhCache与我的Spring应用程序集成,以使其从我的/WEB-INF/目录中加载ehcache.xml文件,并根据给定名称自动装入一个缓存到我的MyUniqueService控制器中?
4个回答

17

首先,您需要在应用上下文中创建一个Ehcache CacheManager单例,如下所示:

<bean id="myEhCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation" value="classpath:my-ehcache.xml"/>
</bean>

这里configLocation被设置为从类路径加载或使用value="/WEB-INF/my-ehcache.xml"

在你的控制器中,只需注入CacheManager实例:

@Controller 
public class MyUniqueService {

    @Resource(name="myEhCacheManager")
    private CacheManager cacheManager;

    ...
}

或者,如果你想完全使用自动装配的方法,请执行:

<bean class="org.springframework.cache.ehcache.EhCacheCacheManager">
    <property name="cacheManager">
        <bean class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
            <property name="configLocation" value="/WEB-INF/ehcache.xml"/>
        </bean>
    </property>
</bean>

按照以下方式设置您的类:

@Controller
public class MyUniqueService { 

    @Autowired
    private org.springframework.cache.CacheManager cacheManager;

    public org.springframework.cache.Cache getUniqueObjectCache() {
        return cacheManager.getCache("uniqueObjectCache");
    }
}

uniqueObjectCache 对应于你在 ehcache.xml 缓存定义中的此缓存实例:

<cache name="uniqueObjectCache"
       maxElementsInMemory="10000"
       eternal="false"
       timeToIdleSeconds="300"
       timeToLiveSeconds="600"
       memoryStoreEvictionPolicy="LRU"
       transactionalMode="off"/>

没有一种方法可以注入一个实际的缓存实例,但是如上所示,您可以注入一个缓存管理器并使用它来获取您感兴趣的缓存。


17

假设您已定义了cacheManager:

<bean id="cacheManager"
    class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation" value="classpath:/ehcache.xml"/>
</bean>

你可以像这样获取/注入特定的缓存:

@Value("#{cacheManager.getCache('myCacheName')}")
private Cache myCache;

如果您感兴趣,还可以查看如何在@Value()中使用Spring EL的示例:http://www.mkyong.com/spring3/spring-el-method-invocation-example/


11

如果上下文可以找到一个正确类的bean,你也可以使用自动装配。这是我如何配置我的XML文件:

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation">
        <value>WEB-INF/ehcache.xml</value>
    </property>
</bean>

<bean id="cache" class="net.sf.ehcache.Cache" factory-bean="cacheManager" factory-method="getCache">
    <constructor-arg value="CacheNameHere" />          
</bean>

还有我的Java类

@Autowired
private net.sf.ehcache.Cache cache;

这个设置对我很有效。


1
@ClintonBosch - 如果您有多个,只需在@Autowired中使用@Qualifier('cacheBeanId')即可。 - moshen

9

确实!或者,如果您想使用Java配置类:

        @Inject
        private ResourceLoader resourceLoader;

        @Bean
        public CacheManager cacheManager() {
            EhCacheCacheManager ehCacheCacheManager = new EhCacheCacheManager();
            try {
                ehCacheCacheManager.setCacheManager(ehcacheCacheManager().getObject());
            } catch (Exception e) {
                throw new IllegalStateException("Failed to create an EhCacheManagerFactoryBean", e);
            }
            return ehCacheCacheManager;
        }

        @Bean
        public FactoryBean<net.sf.ehcache.CacheManager> ehcacheCacheManager() {
            EhCacheManagerFactoryBean bean = new EhCacheManagerFactoryBean();
            bean.setConfigLocation(resourceLoader.getResource("classpath:ehcache.xml"));
            return bean;
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接