Class EhCacheFactoryBean
- java.lang.Object
- net.sf.ehcache.config.CacheConfiguration
- org.springframework.cache.ehcache.EhCacheFactoryBean
- All Implemented Interfaces:
Cloneable,Aware,BeanNameAware,FactoryBean<Ehcache>,InitializingBean
public class EhCacheFactoryBean extends CacheConfiguration implements FactoryBean<Ehcache>, BeanNameAware, InitializingBean
FactoryBeanthat creates a named EhCacheCacheinstance (or a decorator that implements theEhcacheinterface), representing a cache region within an EhCacheCacheManager.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 the CacheManager.
Note: As of Spring 4.1, Spring's EhCache support requires EhCache 2.5 or higher.
- Since:
- 1.1.1
- Author:
- Juergen Hoeller, Dmitriy Kopylenko
- See Also:
setCacheManager(net.sf.ehcache.CacheManager),EhCacheManagerFactoryBean,Cache
Nested Class Summary
Nested classes/interfaces inherited from class net.sf.ehcache.config.CacheConfiguration
CacheConfiguration.BootstrapCacheLoaderFactoryConfiguration, CacheConfiguration.CacheDecoratorFactoryConfiguration, CacheConfiguration.CacheEventListenerFactoryConfiguration, CacheConfiguration.CacheExceptionHandlerFactoryConfiguration, CacheConfiguration.CacheExtensionFactoryConfiguration, CacheConfiguration.CacheLoaderFactoryConfiguration, CacheConfiguration.TransactionalMode
Field Summary
Fields inherited from class net.sf.ehcache.config.CacheConfiguration
bootstrapCacheLoaderFactoryConfiguration, cacheDecoratorConfigurations, cacheEventListenerConfigurations, cacheExceptionHandlerFactoryConfiguration, cacheExtensionConfigurations, cacheLoaderConfigurations, cacheLoaderTimeoutMillis, cacheWriterConfiguration, clearOnFlush, DEFAULT_CACHE_WRITER_CONFIGURATION, DEFAULT_CLEAR_ON_FLUSH, DEFAULT_COPY_ON_READ, DEFAULT_COPY_ON_WRITE, DEFAULT_COPY_STRATEGY_CONFIGURATION, DEFAULT_DISK_ACCESS_STRIPES, DEFAULT_DISK_PERSISTENT, DEFAULT_ETERNAL_VALUE, DEFAULT_EXPIRY_THREAD_INTERVAL_SECONDS, DEFAULT_LOGGING, DEFAULT_MAX_BYTES_OFF_HEAP, DEFAULT_MAX_BYTES_ON_DISK, DEFAULT_MAX_BYTES_ON_HEAP, DEFAULT_MAX_ELEMENTS_ON_DISK, DEFAULT_MAX_ENTRIES_IN_CACHE, DEFAULT_MEMORY_STORE_EVICTION_POLICY, DEFAULT_SPOOL_BUFFER_SIZE, DEFAULT_STATISTICS, DEFAULT_TRANSACTIONAL_MODE, DEFAULT_TTI, DEFAULT_TTL, diskAccessStripes, diskExpiryThreadIntervalSeconds, diskPersistent, diskSpoolBufferSizeMB, eternal, listeners, logging, maxElementsOnDisk, maxEntriesInCache, maxEntriesLocalHeap, memoryStoreEvictionPolicy, name, overflowToDisk, overflowToOffHeap, pinningConfiguration, terracottaConfiguration, timeToIdleSeconds, timeToLiveSeconds
Constructor Summary
Constructors Constructor Description EhCacheFactoryBean()
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidafterPropertiesSet()Invoked by the containingBeanFactoryafter it has set all bean properties and satisfiedBeanFactoryAware,ApplicationContextAwareetc.protected CachecreateCache()Create a raw Cache object based on the configuration of this FactoryBean.protected EhcachedecorateCache(Ehcache cache)Decorate the given Cache, if necessary.EhcachegetObject()Return an instance (possibly shared or independent) of the object managed by this factory.Class<? extends Ehcache>getObjectType()Predict the particularEhcacheimplementation that will be returned fromgetObject()based on logic increateCache()anddecorateCache(Ehcache)as orchestrated byafterPropertiesSet().booleanisSingleton()Is the object managed by this factory a singleton? That is, willFactoryBean.getObject()always return the same object (a reference that can be cached)?voidsetBeanName(String name)Set the name of the bean in the bean factory that created this bean.voidsetBlocking(boolean blocking)Set whether to use a blocking cache that lets read attempts block until the requested element is created.voidsetBootstrapCacheLoader(BootstrapCacheLoader bootstrapCacheLoader)Set an EhCacheBootstrapCacheLoaderfor this cache, if any.voidsetCacheEntryFactory(CacheEntryFactory cacheEntryFactory)Set an EhCacheCacheEntryFactoryto use for a self-populating cache.voidsetCacheEventListeners(Set<CacheEventListener> cacheEventListeners)Specify EhCachecache event listenersto registered with this cache.voidsetCacheManager(CacheManager cacheManager)Set a CacheManager from which to retrieve a named Cache instance.voidsetCacheName(String cacheName)Set a name for which to retrieve or create a cache instance.voidsetDisabled(boolean disabled)Set whether this cache should be marked as disabled.voidsetDiskSpoolBufferSize(int diskSpoolBufferSize)voidsetSampledStatisticsEnabled(boolean sampledStatisticsEnabled)Set whether to enable EhCache's sampled statistics on this cache.voidsetStatisticsEnabled(boolean statisticsEnabled)Set whether to enable EhCache statistics on this cache.voidsetTimeToIdle(int timeToIdle)voidsetTimeToLive(int timeToLive)Methods inherited from class net.sf.ehcache.config.CacheConfiguration
addBootstrapCacheLoaderFactory, addCacheDecoratorFactory, addCacheEventListenerFactory, addCacheExceptionHandlerFactory, addCacheExtensionFactory, addCacheLoaderFactory, addCacheWriter, addConfigurationListener, addCopyStrategy, addDynamicSearchListener, addElementValueComparator, addPersistence, addPinning, addSearchable, addSizeOfPolicy, addTerracotta, bootstrapCacheLoaderFactory, cacheEventListenerFactory, cacheExceptionHandlerFactory, cacheExtensionFactory, cacheLoaderFactory, cacheWriter, clearOnFlush, clone, copyOnRead, copyOnWrite, diskAccessStripes, diskExpiryThreadIntervalSeconds, diskPersistent, diskSpoolBufferSizeMB, dynamicAttributeExtractor, eternal, freezeConfiguration, getBootstrapCacheLoaderFactoryConfiguration, getCacheDecoratorConfigurations, getCacheEventListenerConfigurations, getCacheExceptionHandlerFactoryConfiguration, getCacheExtensionConfigurations, getCacheLoaderConfigurations, getCacheLoaderTimeoutMillis, getCacheWriterConfiguration, getClassLoader, getCopyStrategy, getCopyStrategyConfiguration, getDiskAccessStripes, getDiskExpiryThreadIntervalSeconds, getDiskSpoolBufferSizeMB, getDynamicExtractor, getElementValueComparatorConfiguration, getLogging, getMaxBytesLocalDisk, getMaxBytesLocalDiskAsString, getMaxBytesLocalDiskPercentage, getMaxBytesLocalHeap, getMaxBytesLocalHeapAsString, getMaxBytesLocalHeapPercentage, getMaxBytesLocalOffHeap, getMaxBytesLocalOffHeapAsString, getMaxBytesLocalOffHeapPercentage, getMaxElementsInMemory, getMaxElementsOnDisk, getMaxEntriesInCache, getMaxEntriesLocalDisk, getMaxEntriesLocalHeap, getMaxMemoryOffHeap, getMaxMemoryOffHeapInBytes, getMemoryStoreEvictionPolicy, getName, getPersistenceConfiguration, getPinningConfiguration, getSearchable, getSearchAttributes, getSizeOfPolicyConfiguration, getStatistics, getTerracottaConfiguration, getTerracottaConsistency, getTimeToIdleSeconds, getTimeToLiveSeconds, getTransactionalMode, internalSetDiskCapacity, internalSetEternal, internalSetLogging, internalSetMaxBytesLocalOffheap, internalSetMaxEntriesInCache, internalSetMemCapacity, internalSetMemCapacityInBytes, internalSetOverflowToOffheap, internalSetTimeToIdle, internalSetTimeToLive, isClearOnFlush, isCopyOnRead, isCopyOnWrite, isCountBasedTuned, isDiskPersistent, isEternal, isFrozen, isLocalTransactional, isMaxBytesLocalDiskPercentageSet, isMaxBytesLocalHeapPercentageSet, isMaxBytesLocalOffHeapPercentageSet, isOverflowToDisk, isOverflowToOffHeap, isOverflowToOffHeapSet, isSearchable, isTerracottaClustered, isXaStrictTransactional, isXaTransactional, logging, maxBytesLocalDisk, maxBytesLocalHeap, maxBytesLocalOffHeap, maxElementsInMemory, maxElementsOnDisk, maxEntriesInCache, maxEntriesLocalDisk, maxEntriesLocalHeap, maxMemoryOffHeap, memoryStoreEvictionPolicy, memoryStoreEvictionPolicy, name, overflowToDisk, overflowToOffHeap, persistence, pinning, removeConfigurationListener, searchable, setCacheLoaderTimeoutMillis, setClassLoader, setClearOnFlush, setCopyOnRead, setCopyOnWrite, setDiskAccessStripes, setDiskExpiryThreadIntervalSeconds, setDiskPersistent, setDiskSpoolBufferSizeMB, setDynamicAttributesExtractor, setEternal, setLogging, setMaxBytesLocalDisk, setMaxBytesLocalDisk, setMaxBytesLocalHeap, setMaxBytesLocalHeap, setMaxBytesLocalOffHeap, setMaxBytesLocalOffHeap, setMaxElementsInMemory, setMaxElementsOnDisk, setMaxEntriesInCache, setMaxEntriesLocalDisk, setMaxEntriesLocalHeap, setMaxMemoryOffHeap, setMemoryStoreEvictionPolicy, setMemoryStoreEvictionPolicyFromObject, setName, setOverflowToDisk, setOverflowToOffHeap, setStatistics, setTimeToIdleSeconds, setTimeToLiveSeconds, setTransactionalMode, setupFor, setupFor, sizeOfPolicy, statistics, terracotta, timeoutMillis, timeToIdleSeconds, timeToLiveSeconds, transactionalMode, transactionalMode, validate, validateCompleteConfiguration, validateConfiguration
Constructor Detail
EhCacheFactoryBean
public EhCacheFactoryBean()
Method Detail
setCacheManager
public void setCacheManager(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.
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.
setTimeToLive
public void setTimeToLive(int timeToLive)
setTimeToIdle
public void setTimeToIdle(int timeToIdle)
setDiskSpoolBufferSize
public void setDiskSpoolBufferSize(int diskSpoolBufferSize)
setBlocking
public void setBlocking(boolean blocking)
Set whether to use a blocking cache that lets read attempts block until the requested element is created.If you intend to build a self-populating blocking cache, consider specifying a
CacheEntryFactory.
setCacheEntryFactory
public void setCacheEntryFactory(CacheEntryFactory cacheEntryFactory)
Set an EhCacheCacheEntryFactoryto use for a self-populating cache. If such a factory is specified, the cache will be decorated with EhCache'sSelfPopulatingCache.The specified factory can be of type
UpdatingCacheEntryFactory, which will lead to the use of anUpdatingSelfPopulatingCache.Note: Any such self-populating cache is automatically a blocking cache.
setBootstrapCacheLoader
public void setBootstrapCacheLoader(BootstrapCacheLoader bootstrapCacheLoader)
Set an EhCacheBootstrapCacheLoaderfor this cache, if any.
setCacheEventListeners
public void setCacheEventListeners(Set<CacheEventListener> cacheEventListeners)
Specify EhCachecache event listenersto registered with this cache.
setStatisticsEnabled
public void setStatisticsEnabled(boolean statisticsEnabled)
Set whether to enable EhCache statistics on this cache.Note: As of EhCache 2.7, statistics are enabled by default, and cannot be turned off. This setter therefore has no effect in such a scenario.
- See Also:
net.sf.ehcache.Ehcache#setStatisticsEnabled
setSampledStatisticsEnabled
public void setSampledStatisticsEnabled(boolean sampledStatisticsEnabled)
Set whether to enable EhCache's sampled statistics on this cache.Note: As of EhCache 2.7, statistics are enabled by default, and cannot be turned off. This setter therefore has no effect in such a scenario.
- See Also:
net.sf.ehcache.Ehcache#setSampledStatisticsEnabled
setDisabled
public void setDisabled(boolean disabled)
Set whether this cache should be marked as disabled.- See Also:
Cache.setDisabled(boolean)
setBeanName
public void setBeanName(String name)
Description copied from interface:BeanNameAwareSet 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 such as
InitializingBean.afterPropertiesSet()or a custom init-method.- Specified by:
setBeanNamein interfaceBeanNameAware- Parameters:
name- the name of the bean in the factory. Note that this name is the actual bean name used in the factory, which may differ from the originally specified name: in particular for inner bean names, the actual bean name might have been made unique through appending "#..." suffixes. Use theBeanFactoryUtils.originalBeanName(String)method to extract the original bean name (without suffix), if desired.
afterPropertiesSet
public void afterPropertiesSet() throws CacheException
Description copied from interface:InitializingBeanInvoked by the containingBeanFactoryafter it has set all bean properties and satisfiedBeanFactoryAware,ApplicationContextAwareetc.This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.
- Specified by:
afterPropertiesSetin interfaceInitializingBean- Throws:
CacheException
createCache
protected Cache createCache()
Create a raw Cache object based on the configuration of this FactoryBean.
decorateCache
protected Ehcache decorateCache(Ehcache cache)
Decorate the given Cache, if necessary.- Parameters:
cache- the raw Cache object, based on the configuration of this FactoryBean- Returns:
- the (potentially decorated) cache object to be registered with the CacheManager
getObject
public Ehcache getObject()
Description copied from interface:FactoryBeanReturn 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 FactoryBean is not fully initialized yet at the time of the call (for example because it is involved in a circular reference), throw a corresponding
FactoryBeanNotInitializedException.As of Spring 2.0, FactoryBeans are allowed to return
nullobjects. The factory will consider this as normal value to be used; it will not throw a FactoryBeanNotInitializedException in this case anymore. FactoryBean implementations are encouraged to throw FactoryBeanNotInitializedException themselves now, as appropriate.- Specified by:
getObjectin interfaceFactoryBean<Ehcache>- Returns:
- an instance of the bean (can be
null) - See Also:
FactoryBeanNotInitializedException
getObjectType
public Class<? extends Ehcache> getObjectType()
Predict the particularEhcacheimplementation that will be returned fromgetObject()based on logic increateCache()anddecorateCache(Ehcache)as orchestrated byafterPropertiesSet().- Specified by:
getObjectTypein interfaceFactoryBean<Ehcache>- Returns:
- the type of object that this FactoryBean creates, or
nullif not known at the time of the call - See Also:
ListableBeanFactory.getBeansOfType(java.lang.Class<T>)
isSingleton
public boolean isSingleton()
Description copied from interface:FactoryBeanIs the object managed by this factory a singleton? That is, willFactoryBean.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 returntrueunless 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.
NOTE: This method returning
falsedoes not necessarily indicate that returned objects are independent instances. An implementation of the extendedSmartFactoryBeaninterface may explicitly indicate independent instances through itsSmartFactoryBean.isPrototype()method. PlainFactoryBeanimplementations which do not implement this extended interface are simply assumed to always return independent instances if theisSingleton()implementation returnsfalse.- Specified by:
isSingletonin interfaceFactoryBean<Ehcache>- Returns:
- whether the exposed object is a singleton
- See Also:
FactoryBean.getObject(),SmartFactoryBean.isPrototype()