使用 Spring 获取 EhCache 实例......智能地

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11462662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:15:48  来源:igfitidea点击:

Getting an EhCache instance with Spring... intelligently

springehcache

提问by Naftuli Kay

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?

我需要按名称获取特定的 EhCache 实例,如果可能,我更喜欢自动装配。鉴于以下自动配置的控制器,我如何在我正在寻找的缓存实例中自动装配?

@Controller 
public class MyUniqueService {
    ...
}


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

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?我没有看到来自 EhCache 的任何关于它在我的/WEB-INF/目录中加载 ehcache.xml 文件的日志消息。我如何让它加载它?

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

我如何将 EhCache 与我的 Spring 应用程序集成,让它ehcache.xml从我的/WEB-INF/目录加载文件并按给定名称将缓存自动装配到我的MyUniqueService控制器中?

回答by jeha

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

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

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

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

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

In your controller simply inject the CacheManagerinstance:

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

@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>

Setup your class like so:

像这样设置你的类:

@Controller
public class MyUniqueService { 

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

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

uniqueObjectCachecorresponds to this cache instance in your ehcache.xmlcache definition:

uniqueObjectCache对应于ehcache.xml缓存定义中的这个缓存实例:

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

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

回答by petrsyn

Assuming you have cacheManager defined:

假设您定义了 cacheManager:

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

You can get/inject specific cache like this:

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

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

See also examples how to use Spring EL inside the @Value()http://www.mkyong.com/spring3/spring-el-method-invocation-example/if you are interested.

如果您有兴趣,另请参阅如何在@Value()http://www.mkyong.com/spring3/spring-el-method-invocation-example/ 中使用 Spring EL 的示例。

回答by Bryant Larsen

You can also use autowire if the context can find a bean with the correct class. Here is how I configured my xml

如果上下文可以找到具有正确类的 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>

And my java class

还有我的java类

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

This setup works for me.

这个设置对我有用。

回答by Vincent Devillers

Indeed! Or if you want to use a java config class:

的确!或者,如果您想使用 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;
        }