通过Spring智能地获取EhCache实例... [英] Getting an EhCache instance with Spring... intelligently

查看:141
本文介绍了通过Spring智能地获取EhCache实例...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要按名称获取特定的EhCache实例,并且如果可能的话,我希望自动装配.给定以下自动配置的控制器,如何在需要的缓存实例中自动布线?

I need to get a specific EhCache instance by name and I'd prefer to autowire if possible. Given the following automatically configured controller, how can I autowire in the cache instance I'm looking for?

@Controller 
public class MyUniqueService {
    ...
}


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

如何在应用程序上下文中配置EhCache?我没有看到来自EhCache的任何日志消息,有关它在我的/WEB-INF/目录中加载ehcache.xml文件的信息.如何加载它?

How do I configure EhCache in my application context? I don't see any log messages from EhCache about it loading the ehcache.xml file in my /WEB-INF/ directory. How do I make it load it?

如何将EhCache与我的Spring应用程序集成,以使其从我的/WEB-INF/目录加载ehcache.xml文件,并将具有给定名称的缓存自动连线到我的MyUniqueService控制器中?

How can I integrate EhCache with my Spring application to have it load the ehcache.xml file from my /WEB-INF/ directory and autowire a cache by a given name into my MyUniqueService controller?

推荐答案

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

First you need to create a Ehcache CacheManager singleton in you app context like this:

<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".

Here configLocation is set to load from classpath or use value="/WEB-INF/my-ehcache.xml".

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

In your controller simply inject the CacheManager instance:

@Controller 
public class MyUniqueService {

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

    ...
}


或者,如果您想采用完全自动布线"路线,请执行以下操作:


Alternatively, if you'd like to go the "entirely autowired" route, do:

<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缓存定义中的该缓存实例:

uniqueObjectCache corresponds to this cache instance in your ehcache.xml cache definition:

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

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

There isn't a way to inject an actual cache instance, but as shown above, you can inject a cache manager and use it to get the cache you're interested in.

这篇关于通过Spring智能地获取EhCache实例...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆