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

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

  1. 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 represente ...

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

  3. 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 evi ...

  4. Ehcache(2.9.x) - API Developer Guide, Basic Caching

    Creating a CacheManager All usages of the Ehcache API start with the creation of a CacheManager. The ...

  5. Ehcache(2.9.x) - API Developer Guide, Cache Usage Patterns

    There are several common access patterns when using a cache. Ehcache supports the following patterns ...

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

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

    About Explicit Locking Ehcache contains an implementation which provides for explicit locking, using ...

  8. Ehcache(2.9.x) - API Developer Guide, Transaction Support

    About Transaction Support Transactions are supported in versions of Ehcache 2.0 and higher. The 2.3. ...

  9. 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 applie ...

随机推荐

  1. 启动报错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

    如果你是maven项目,tomcat在发布项目的时候没有同时发布maven依赖所添加的jar包,你需要设置一下eclipse:项目 —> 属性 -> Deployment Assembly ...

  2. fixed 定位 苹果手机输入框触发时内容全部隐藏

    问题出现在东钿微信公众号用户注册页面 页面中只有两个输入框 页面没有超过一屏,悬浮按钮也要出现在本页面 ,开始布局页面的时候没什么问题,然后我在我自己手机上测试 ,输入手机号码,非常奇怪的问题出现了, ...

  3. PicklingError: Can't pickle <type 'generator'>: it's not found as __builtin_

    多进程传递 参数时,需要是python系统已知的,不然不知道怎么序列化

  4. map的正确删除方式

    遍历删除map元素的正确方式是 for(itor = maptemplate.begin; itor != maptemplate.end(); ) {      if(neederase)     ...

  5. VS清除缓存

    今天不小心在项目里面把一个 ==  写成了 =,结果数据一下子崩溃了. 后来测试,发现,换一个编译环境,或者换一个编译模式比如debug改成release,就好使了. 1 测试流程 2 测试数据 3 ...

  6. python中List操作

    传送门 官方文件地址 list.append(x): 将x加入列表尾部,等价于a[len(a):] = [x] 例: >>> list1=[1,2,3,4] >>> ...

  7. 发布方配ASP.NET网站服务器

    方配ASP.NET网站服务器是一款简单,轻量,灵活的ASP.NET网站服务器,使用它可以无需安装复杂的IIS,直接就可以运行ASP.NET网站,使用非常简单,把exe文件拷贝到ASP.NET的网站目录 ...

  8. Apache POI 合并单元格

    合并单元格所使用的方法: sheet.addMergedRegion( CellRangeAddress  cellRangeAddress  );   CellRangeAddress  对象的构造 ...

  9. andriod开发,简单的封装网络请求并监听返回.

    一.为什么封装 因为android 4.0 以后的发送网络请求必须要放到异步线程中,而异步线程必须跟handle合作才能更新主线程中的UI,所以建议用一个类继承handler来异步处理网络请求. 二. ...

  10. BZOJ 2763: [JLOI2011]飞行路线 最短路

    2763: [JLOI2011]飞行路线 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...