All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.cache.ehcache.EhCacheManagerFactoryBean Maven / Gradle / Ivy

There is a newer version: 5.3.34
Show newest version
/*
 * Copyright 2002-2006 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.cache.ehcache;

import java.io.IOException;

import net.sf.ehcache.CacheException;
import net.sf.ehcache.CacheManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;

/**
 * FactoryBean that exposes an EHCache {@link net.sf.ehcache.CacheManager} instance
 * (independent or shared), configured from a specified config location.
 *
 * 

If no config location is specified, a CacheManager will be configured from * "ehcache.xml" in the root of the class path (that is, default EHCache initialization * - as defined in the EHCache docs - will apply). * *

Setting up a separate EhCacheManagerFactoryBean is also advisable when using * EhCacheFactoryBean, as it provides a (by default) independent CacheManager instance * and cares for proper shutdown of the CacheManager. EhCacheManagerFactoryBean is * also necessary for loading EHCache configuration from a non-default config location. * *

Note: As of Spring 2.0, this FactoryBean will by default create an independent * CacheManager instance, which requires EHCache 1.2 or higher. Set the "shared" * flag to "true" to create a CacheManager instance that is shared at the VM level * (which is also compatible with EHCache 1.1). * * @author Dmitriy Kopylenko * @author Juergen Hoeller * @since 1.1.1 * @see #setConfigLocation * @see #setShared * @see EhCacheFactoryBean * @see net.sf.ehcache.CacheManager */ public class EhCacheManagerFactoryBean implements FactoryBean, InitializingBean, DisposableBean { protected final Log logger = LogFactory.getLog(getClass()); private Resource configLocation; private boolean shared = false; private CacheManager cacheManager; /** * Set the location of the EHCache config file. A typical value is "/WEB-INF/ehcache.xml". *

Default is "ehcache.xml" in the root of the class path, or if not found, * "ehcache-failsafe.xml" in the EHCache jar (default EHCache initialization). */ public void setConfigLocation(Resource configLocation) { this.configLocation = configLocation; } /** * Set whether the EHCache CacheManager should be shared (as a singleton at the VM level) * or independent (typically local within the application). Default is "false", creating * an independent instance. *

Note that independent CacheManager instances are only available on EHCache 1.2 and * higher. Switch this flag to "true" if you intend to run against an EHCache 1.1 jar. */ public void setShared(boolean shared) { this.shared = shared; } public void afterPropertiesSet() throws IOException, CacheException { logger.info("Initializing EHCache CacheManager"); if (this.shared) { // Shared CacheManager singleton at the VM level. if (this.configLocation != null) { this.cacheManager = CacheManager.create(this.configLocation.getInputStream()); } else { this.cacheManager = CacheManager.create(); } } else { // Independent CacheManager instance (the default). if (this.configLocation != null) { this.cacheManager = new CacheManager(this.configLocation.getInputStream()); } else { this.cacheManager = new CacheManager(); } } } public Object getObject() { return this.cacheManager; } public Class getObjectType() { return (this.cacheManager != null ? this.cacheManager.getClass() : CacheManager.class); } public boolean isSingleton() { return true; } public void destroy() { logger.info("Shutting down EHCache CacheManager"); this.cacheManager.shutdown(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy