About Class Loading
Class loading, within the plethora of environments that Ehcache can is running, could be complex. but with the Ehcache, all class loading are done in a standard-of-the-utility class:classloaderutil.
Plugin Class Loading
Ehcache allows plugins for events and distribution. These is loaded and created as follows:
/*** Creates a new class instance. Logs errors along the. Classes is loaded * using the Ehcache standard ClassLoader. * * @paramClassName a fully qualified class name *@returnNULL if the instance cannot be loaded*/ Public StaticObject createnewinstance (String className)throwscacheexception {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 NewCacheexception ("Unable to load class" + ClassName + ". Initial cause was "+e.getmessage (), E); } } Try{newinstance=clazz.newinstance (); } Catch(illegalaccessexception e) {Throw NewCacheexception ("Unable to load class" + ClassName + ". Initial cause was "+e.getmessage (), E); } Catch(instantiationexception e) {Throw NewCacheexception ("Unable to load class" + ClassName + ". Initial cause was "+e.getmessage (), E); } returnnewinstance;} /*** Gets The ClassLoader that all classes in Ehcache, and extensions, * should with for classloading. All classloading the Ehcache should use the This * one. This was the only thing, seems to work for all of the class * loading situations found in the wild. * @returnThe thread context class loader. */ Public StaticClassLoader Getstandardclassloader () {returnThread.CurrentThread (). Getcontextclassloader ();} /*** Gets A fallback ClassLoader that all classes in Ehcache, and * extensions, should with for classloading. This is used if the * Context class loader does isn't work. * @returnThe ClassLoaderUtil.class.getClassLoader ();*/ Public StaticClassLoader Getfallbackclassloader () {returnClassloaderutil.class. getClassLoader ();}
If This does isn't work for some reason, a cacheexception are 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 is placed at the root of the classpath (i.e., not In any package).
Ehcache (2.9.x)-API Developer guide, Class Loading