用Spring获取EhCache实例......聪明地

时间:2012-07-13 00:40:03

标签: spring ehcache

我需要按名称获取特定的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应用程序集成,以便从我的ehcache.xml目录加载/WEB-INF/文件,并通过给定名称将缓存自动装入我的MyUniqueService控制器?

4 个答案:

答案 0 :(得分: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"/>

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

答案 1 :(得分: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() http://www.mkyong.com/spring3/spring-el-method-invocation-example/中如何使用Spring EL的示例。

答案 2 :(得分:11)

如果上下文可以找到具有正确类的bean,您也可以使用autowire。以下是我配置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;

此设置适用于我。

答案 3 :(得分:7)

事实上!或者如果你想使用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;
        }