org.springframework.cache.ehcache
Class EhCacheFactoryBean

java.lang.Object
  extended by org.springframework.cache.ehcache.EhCacheFactoryBean
All Implemented Interfaces:
BeanNameAware, FactoryBean, InitializingBean

public class EhCacheFactoryBean
extends Object
implements FactoryBean, BeanNameAware, InitializingBean

FactoryBean that creates a named EHCache Cache object, representing a cache region.

If the specified named cache is not configured in the cache configuration descriptor, this FactoryBean will construct an instance of a Cache with the provided name and the specified cache properties and add it to the CacheManager for later retrieval. If some or all properties are not set at configuration time, this FactoryBean will use defaults.

Note: If the named Cache instance is found, the properties will be ignored and the Cache instance will be retrieved from CacheManager.

Since:
1.1.1
Author:
Dmitriy Kopylenko, Juergen Hoeller
See Also:
setCacheManager(net.sf.ehcache.CacheManager), EhCacheManagerFactoryBean, Cache

Field Summary
protected  Log logger
           
 
Constructor Summary
EhCacheFactoryBean()
           
 
Method Summary
 void afterPropertiesSet()
          Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).
 Object getObject()
          Return an instance (possibly shared or independent) of the object managed by this factory.
 Class getObjectType()
          Return the type of object that this FactoryBean creates, or null if not known in advance.
 boolean isSingleton()
          Is the bean managed by this factory a singleton or a prototype?
 void setBeanName(String name)
          Set the name of the bean in the bean factory that created this bean.
 void setCacheManager(net.sf.ehcache.CacheManager cacheManager)
          Set a CacheManager from which to retrieve a named Cache instance.
 void setCacheName(String cacheName)
          Set a name for which to retrieve or create a cache instance.
 void setDiskExpiryThreadIntervalSeconds(int diskExpiryThreadIntervalSeconds)
          Set the number of seconds between runs of the disk expiry thread.
 void setDiskPersistent(boolean diskPersistent)
          Set whether the disk store persists between restarts of the Virtual Machine.
 void setEternal(boolean eternal)
          Set whether elements are eternal.
 void setMaxElementsInMemory(int maxElementsInMemory)
          Specify the maximum number of cached objects in memory.
 void setOverflowToDisk(boolean overflowToDisk)
          Set whether elements can overflow to disk when the in-memory cache has reached the maximum size limit.
 void setTimeToIdle(int timeToIdle)
          Set the time in seconds to idle for an element before it expires i.e. the maximum amount of time between accesses before an element expires.
 void setTimeToLive(int timeToLive)
          Set t he time in seconds to live for an element before it expires, i.e. the maximum time between creation time and when an element expires.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected final Log logger
Constructor Detail

EhCacheFactoryBean

public EhCacheFactoryBean()
Method Detail

setCacheManager

public void setCacheManager(net.sf.ehcache.CacheManager cacheManager)
Set a CacheManager from which to retrieve a named Cache instance. By default, CacheManager.getInstance() will be called.

Note that in particular for persistent caches, it is advisable to properly handle the shutdown of the CacheManager: Set up a separate EhCacheManagerFactoryBean and pass a reference to this bean property.

A separate EhCacheManagerFactoryBean is also necessary for loading EHCache configuration from a non-default config location.

See Also:
EhCacheManagerFactoryBean, CacheManager.getInstance()

setCacheName

public void setCacheName(String cacheName)
Set a name for which to retrieve or create a cache instance. Default is the bean name of this EhCacheFactoryBean.


setMaxElementsInMemory

public void setMaxElementsInMemory(int maxElementsInMemory)
Specify the maximum number of cached objects in memory. Default is 10000 elements.


setOverflowToDisk

public void setOverflowToDisk(boolean overflowToDisk)
Set whether elements can overflow to disk when the in-memory cache has reached the maximum size limit. Default is "true".


setEternal

public void setEternal(boolean eternal)
Set whether elements are eternal. If true, timeouts are ignored and the element is never expired. Default is "false".


setTimeToLive

public void setTimeToLive(int timeToLive)
Set t he time in seconds to live for an element before it expires, i.e. the maximum time between creation time and when an element expires. It is only used if the element is not eternal. Default is 120 seconds.


setTimeToIdle

public void setTimeToIdle(int timeToIdle)
Set the time in seconds to idle for an element before it expires i.e. the maximum amount of time between accesses before an element expires. It is only used if the element is not eternal. Default is 120 seconds.


setDiskPersistent

public void setDiskPersistent(boolean diskPersistent)
Set whether the disk store persists between restarts of the Virtual Machine. The default is false.


setDiskExpiryThreadIntervalSeconds

public void setDiskExpiryThreadIntervalSeconds(int diskExpiryThreadIntervalSeconds)
Set the number of seconds between runs of the disk expiry thread. The default is 120 seconds.


setBeanName

public void setBeanName(String name)
Description copied from interface: BeanNameAware
Set the name of the bean in the bean factory that created this bean.

Invoked after population of normal bean properties but before an init callback like InitializingBean's afterPropertiesSet or a custom init-method.

Specified by:
setBeanName in interface BeanNameAware
Parameters:
name - the name of the bean in the factory

afterPropertiesSet

public void afterPropertiesSet()
                        throws net.sf.ehcache.CacheException,
                               IOException
Description copied from interface: InitializingBean
Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).

This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.

Specified by:
afterPropertiesSet in interface InitializingBean
Throws:
net.sf.ehcache.CacheException
IOException

getObject

public Object getObject()
Description copied from interface: FactoryBean
Return an instance (possibly shared or independent) of the object managed by this factory. As with a BeanFactory, this allows support for both the Singleton and Prototype design pattern.

If this method returns null, the factory will consider the FactoryBean as not fully initialized and throw a corresponding FactoryBeanNotInitializedException.

Specified by:
getObject in interface FactoryBean
Returns:
an instance of the bean (should not be null; a null value will be considered as an indication of incomplete initialization)
See Also:
FactoryBeanNotInitializedException

getObjectType

public Class getObjectType()
Description copied from interface: FactoryBean
Return the type of object that this FactoryBean creates, or null if not known in advance. This allows to check for specific types of beans without instantiating objects, for example on autowiring.

For a singleton, this should try to avoid singleton creation as far as possible; it should rather estimate the type in advance. For prototypes, returning a meaningful type here is advisable too.

This method can be called before this FactoryBean has been fully initialized. It must not rely on state created during initialization; of course, it can still use such state if available.

NOTE: Autowiring will simply ignore FactoryBeans that return null here. Therefore it is highly recommended to implement this method properly, using the current state of the FactoryBean.

Specified by:
getObjectType in interface FactoryBean
Returns:
the type of object that this FactoryBean creates, or null if not known at the time of the call
See Also:
ListableBeanFactory.getBeansOfType(java.lang.Class)

isSingleton

public boolean isSingleton()
Description copied from interface: FactoryBean
Is the bean managed by this factory a singleton or a prototype? That is, will getObject() always return the same object (a reference that can be cached)?

NOTE: If a FactoryBean indicates to hold a singleton object, the object returned from getObject() might get cached by the owning BeanFactory. Hence, do not return true unless the FactoryBean always exposes the same reference.

The singleton status of the FactoryBean itself will generally be provided by the owning BeanFactory; usually, it has to be defined as singleton there.

Specified by:
isSingleton in interface FactoryBean
Returns:
if this bean is a singleton
See Also:
FactoryBean.getObject()


Copyright (c) 2002-2007 The Spring Framework Project.