【问题标题】:Getting an EhCache instance with Spring... intelligently用 Spring 智能地获取 EhCache 实例
【发布时间】:2012-07-12 20:13:04
【问题描述】:

我需要按名称获取特定的 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 控制器中?

【问题讨论】:

    标签: spring ehcache


    【解决方案1】:

    首先,您需要在您的应用上下文中创建一个 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"/>
    

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

    【讨论】:

      【解决方案2】:

      假设你已经定义了 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 的示例。

      【讨论】:

        【解决方案3】:

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

        此设置适合我。

        【讨论】:

        • @ClintonBosch - 如果您有多个,只需使用 @Qualifier('cacheBeanId')@Autowired
        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 2022-11-13
          • 1970-01-01
          • 2019-02-07
          • 2015-06-12
          • 1970-01-01
          • 2015-04-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多