Skip to content

Instantly share code, notes, and snippets.

@utsengar
Created June 14, 2011 04:13
Show Gist options
  • Save utsengar/1024302 to your computer and use it in GitHub Desktop.
Save utsengar/1024302 to your computer and use it in GitHub Desktop.
fix singelton issues when using multiple classloaders
private static Class getClass(String classname) throws ClassNotFoundException {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
if(classLoader == null)
classLoader = Singleton.class.getClassLoader();
return (classLoader.loadClass(classname));
}
}
//Source: http://snehaprashant.blogspot.com/2009/01/singleton-pattern-in-java.html
/** The preceding method tries to associate the classloader with the current thread; if that
classloader is null, the method uses the same classloader that loaded a singleton base class. The
preceding method can be used instead of Class.forName(). **/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment