8

我需要按名称获取特定的 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

4 回答 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"/>

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

于 2012-07-13T11:36:57.097 回答
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 的示例。

于 2014-02-05T11:18:11.203 回答
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;

这个设置对我有用。

于 2012-12-17T21:16:27.327 回答
8

的确!或者,如果您想使用 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;
        }
于 2012-07-13T13:00:47.433 回答