Ehcache(2.9.x) - API Developer Guide, Class Loading

About Class Loading

Class loading, within the plethora of environments that Ehcache can be running, could be complex. But with Ehcache, all class loading is done in a standard way in one utility class: ClassLoaderUtil.

Plugin Class Loading

Ehcache allows plugins for events and distribution. These are loaded and created as follows:

/**
 * Creates a new class instance. Logs errors along the way. Classes are loaded
 * using the Ehcache standard classloader.
 *
 * @param className a fully qualified class name
 * @return null if the instance cannot be loaded
 */
public static Object createNewInstance(String className) throws CacheException {
    Class clazz;
    Object newInstance;
    try {
      clazz = Class.forName(className, true, getStandardClassLoader());
    } catch (ClassNotFoundException e) {
      //try fallback
      try {
          clazz = Class.forName(className, true, getFallbackClassLoader());
      } catch (ClassNotFoundException ex) {
          throw new CacheException("Unable to load class " + className +
                     ". Initial cause was " + e.getMessage(), e);
      }
    }
    try {
      newInstance = clazz.newInstance();
    } catch (IllegalAccessException e) {
      throw new CacheException("Unable to load class " + className +
                 ". Initial cause was " + e.getMessage(), e);
    } catch (InstantiationException e) {
      throw new CacheException("Unable to load class " + className +
                 ". Initial cause was " + e.getMessage(), e);
    }
    return newInstance;
}
/**
 * Gets the ClassLoader that all classes in ehcache, and extensions,
 * should use for classloading. All ClassLoading in Ehcache should use this
 * one. This is the only thing that seems to work for all of the class
 * loading situations found in the wild.
 * @return the thread context class loader.
 */
public static ClassLoader getStandardClassLoader() {
    return Thread.currentThread().getContextClassLoader();
}
/**
 * Gets a fallback ClassLoader that all classes in ehcache, and
 * extensions, should use for classloading. This is used if the
 * context class loader does not work.
 * @return the ClassLoaderUtil.class.getClassLoader(); */
public static ClassLoader getFallbackClassLoader() {
    return ClassLoaderUtil.class.getClassLoader();
}

If this does not work for some reason, a CacheException is thrown with a detailed error message.

Loading of ehcache.xml Resources

If the configuration is otherwise unspecified, Ehcache looks for a configuration in the following order:

  • Thread.currentThread().getContextClassLoader().getResource("/ehcache.xml")
  • ConfigurationFactory.class.getResource("/ehcache.xml")
  • ConfigurationFactory.class.getResource("/ehcache-failsafe.xml")

Ehcache uses the first configuration found. Note the use of “/ehcache.xml”, which requires that ehcache.xml be placed at the root of the classpath (i.e., not in any package).

时间: 2024-07-30 13:50:05

Ehcache(2.9.x) - API Developer Guide, Class Loading的相关文章

Ehcache(2.9.x) - API Developer Guide, Cache Eviction Algorithms

About Cache Eviction Algorithms A cache eviction algorithm is a way of deciding which element to evict when the cache is full. In Ehcache , the memory store and the off-heap store might be limited in size. When these stores get full, elements are evi

Ehcache(2.9.x) - API Developer Guide, Write-Through and Write-Behind Caches

About Write-Through and Write-Behind Caches Write-through caching is a caching pattern where writes to the cache cause writes to an underlying resource. The cache acts as a facade to the underlying resource. With this pattern, it often makes sense to

Ehcache(2.9.x) - API Developer Guide, Key Classes and Methods

About the Key Classes Ehcache consists of a CacheManager, which manages logical data sets represented as Caches. A Cache object contains Elements, which are essentially name-value pairs. You can use Cache objects to hold any kind of data that you wan

Ehcache(2.9.x) - API Developer Guide, Searching a Cache

About Searching The Search API allows you to execute arbitrarily complex queries against caches. The development of alternative indexes on values provides the ability for data to be looked up based on multiple criteria instead of just keys. Note: Ter

Ehcache(2.9.x) - API Developer Guide, Using Explicit Locking

About Explicit Locking Ehcache contains an implementation which provides for explicit locking, using read and write locks. With explicit locking, it is possible to get more control over Ehcache 's locking behavior to allow business logic to apply an

Ehcache(2.9.x) - API Developer Guide, Cache Extensions

About Cache Extensions Cache extensions are a general-purpose mechanism to allow generic extensions to a cache. Cache extensions are tied into the cache lifecycle. For that reason, this interface has the lifecycle methods. Cache extensions are create

Ehcache(2.9.x) - API Developer Guide, Blocking and Self Populating Caches

About Blocking and Self-Populating Caches The net.sf.ehcache.constructs package contains some applied caching classes which use the core classes to solve everyday caching problems. Two of these are BlockingCache and SelfPopulatingCache. Blocking Cach

Ehcache(2.9.x) - API Developer Guide, Cache Exception Handlers

About Exception Handlers By default, most cache operations will propagate a runtime CacheException on failure. An interceptor, using a dynamic proxy, may be configured so that a CacheExceptionHandler can be configured to intercept Exceptions. Errors

Ehcache(2.9.x) - API Developer Guide, Cache Decorators

About Cache Decorators Ehcache uses the Ehcache interface, of which Cache is an implementation. It is possible and encouraged to create Ehcache decorators that are backed by a Cache instance, implement Ehcache and provide extra functionality. The Dec