Marcel Stör
Marcel Stör

Reputation: 23525

Spring cache vs. cachemanger

The following sample from the Spring manual confuses the heck out of me.

<bean id="cacheManager" 
      class="org.springframework.cache.ehcache.EhCacheCacheManager"
      p:cache-manager-ref="ehcache"/>

<!-- Ehcache library setup -->
<bean id="ehcache" 
      class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" 
      p:config-location="ehcache.xml"/>

The naming convention mandates that EhCacheManagerFactoryBean produces a cache manager, more precisely it's a net.sf.ehcache.CacheManager instance. Yet, the bean is called ehcache not ehcachemanager. The actual cacheManager, however, references this bean.

In prose you could say that one is the Ehcache cache manager while the other is the Spring cache manager (which is backed by the former).

It gets worse if you use the EhCacheFactoryBean:

<bean
  id="myCache"
  class="org.springframework.cache.ehcache.EhCacheFactoryBean">
  <property name="cacheManager">
    <ref local="ehcache" />
  </property>

There's a property called cacheManager which references a bean called ehcache.

Did I misunderstand anything or is it really that confusing? Bad design or just bad naming in the example?

Upvotes: 9

Views: 11682

Answers (1)

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340693

Spring framework recently introduced caching abstraction with org.springframework.cache.CacheManager central interface. This interface has few built-in implementations, including:

This design allows you to switch caching library without touching application code. As you can see one of these built-in implementations is backed by EhCache. However notice that EhCacheCacheManager is just a bridge between Spring caching abstraction and EhCache library. Thus it needs existing net.sf.ehcache.CacheManager. You can either create an instance of this cache manager yourself or take advantage of existing factory bean, namely EhCacheManagerFactoryBean.

I understand it's confusing because of overlapping names, but it should be clear from the above which classes come from which library and why are they used.

Upvotes: 8

Related Questions