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).

转载于:https://www.cnblogs.com/huey/p/5847310.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值