How to construct a custom ClassLoader Since the custom ClassLoader can solve the above problem, then take a look, how we use custom ClassLoader.
Combined with the original code of this variety - (in the DIFFERENTVERSIONSPUSH directory), there is a FileSystemClassLoader, the class diagram is described below: Figure 9. Look at his method FindclassBytes (String classname); public Byte [] FindClassBytes (String classname) { try {String pathName = currentRoot File.separatorChar className replace (, File.separatorChar '.') ".class";. FileInputStream inFile = new FileInputStream (pathName); byte [] classBytes = new byte [inFile.available ( )]; inFile.read (classBytes); return classBytes;} catch (java.io.IOException ioEx) {return null;}} public Class findClass (String name) throws ClassNotFoundException {byte [] classBytes = findClassBytes (name); if (ClassBytes == null) {throw new classnotfoundexception ();} else {return defineclass (name, classbytes, 0, classbytes.length);}} public class findClass (String name, byte [] classBytes) throws ClassNotFoundException {if (classBytes == null) {throw new ClassNotFoundException ( "(classBytes == null)");} else {return defineClass (name, classBytes, 0, classBytes.length );}} Public void execute (string code) {class klass = null; try {klass = findclass (codename, code); taskintf task = (taskintf) klass.newinstance (); task.execute () ;
} CatCH (Exception Exception) {Exception.PrintStackTrace ();}} This class FileSystemClassLoader is used by the client, used to define the class, and convert it to Byte [], then byte [] RMI Server is executed. (Telling DefineClass () can perform any bytecode, from the compiled file, the network is even the BCEL bytecode engine, in the Server side, but also define the formiTsystemClassLoader to define the form of CLIENT.TASKIMPL in the form of Byte []. . See the CLIENT end code: public class client {public static void main (string [] args) {try {byte [] code = getClassdefinition ("client.taskimpl"); ServerintF.execute ("Client.taskimpl", Code; } catch (RemoteException remoteException) {remoteException.printStackTrace ();.}} private static byte [] getClassDefinition (String codeName) {String userDir = System.getProperties () getProperty ( "BytePath"); FileSystemClassLoader fscl1 = null; try {fscl1 = new FileSystemClassLoader (userDir);} catch (FileNotFoundException fileNotFoundException) {fileNotFoundException.printStackTrace ();} return fscl1.findClassBytes (codeName);}} in the RMI server ServerImpl procedure, the client receives from the byte code (byte [ ]), So FileSystemClassLoader will construct a Class, real example, and execute from byte []. One thing to pay attention to: Every time a client is received, FileSystemClassLoad is re-instantified (you can see in the execution result), which means that Client.Impl is not found in the classpath, but through FileSystemClassLoader. FindClass () to perform defineClass (), so each FileSystemClassLoader is created a new instance, natural define Class is also different. In this way, we can distinguish these two classes in the execution of RMI.
(! Client.TaskImpl = client.TaskImp on the article has been concluded.) See in the server execution code: public void execute (String codeName, byte [] code) throws RemoteException {FileSystemClassLoader fileSystemClassLoader = null; try {fileSystemClassLoader = new FileSystemClassLoader (); fileSystemClassLoader.execute (codeName, code);} catch (Exception exception) {throw new RemoteException (exception.getMessage ());}} execution result of the server side: FIG. 10, the server displays the following two figures respectively It is the client display. Figure 11. Execution of Client1 Show Figure 12. Client2 execution results Ha, the top of the ocean sprinkle so much, the church in one step is how everyone performs "different versions" code in the same VM virtual machine. (These code has the same class name and package name). Class Loaders is applied in J2EE. Here you are not enough to have something you are now. . .
My A_WAR.WAR's Web Project is com.mycom.test and I am in another B_war.war's wenb project is also com.mycom.test and they still work well.
When a large EJB project, a server deploying multiple EJBs, WAR project, they will not affect each other. AppServer will have its own loading strategy, such as the JAR package in your web, which will take precedence over the Appserver itself.
In addition, J2EE's ClassLoader mechanism can be able to refer to this article on TSS.
Understanding J2ee Application Server Class Loading Architectures
resource:
Sample code for this article JDK 1.5 API Docs The Java language specification "Understanding Extension Class Loading" in the Java tutorial "Inside Class Loaders" from ONJava "Inside Class Loaders: Debugging" from ONJava "? What version is your Java code" from JavaWorld "Understanding J2EE Application Server Class Loading Architectures" from TheServerSide Byte Code Engineering Library Server-Based Java Programming by Ted Neward original: http: //www.onjava.com/pub/a/onjava/2005/01/26/classloading.htm