ClassLoader actual combat one, ClassLoader achieve thermal deployment

I. train of thought

Use your class loader to reload the class file

not to say nonsense, directly on the code, do not know where the message can also contact me

Three, code

(1) classes that are monitored (requiring hot deployment)

Public, interface, Message, {void, send ()}
Public, class, MessageImpl, implements, Message, {@Override, public, void, send (), {System.out.println (send message AAAA)}}

(2) custom class loader

Import; import; import; import; import; public class MyClassLoader extends ClassLoader {/ / classloader name private String name private String; / / class loading path path; public MyClassLoader (ClassLoader parent, String name, String path) {super (parent); / / parent classloader = name; this.path = path;} / * * * loadClass method to override the parent class @Override * / public Class< > loadClass (String? Name) throws ClassNotFoundException {Class< > clazz? = null; / / this kind of judgment is not if ( (name)) {/ / lookup is It has been loaded with clazz = this.findLoadedClass (name); if (clazz = = null) {/ / if not found, continue to search for clazz = this.findClass (name); return super.loadClass (name)}};} / * * * * / custom rules override the findClass method, @ Override > protected Class< findClass (String? Name throws ClassNotFoundException) {/ / into binary byte stream, because JVM only know do not know binary string byte[] B = readFileToByteArray (name); return this.defineClass (, B, 0, b.length);} / * * * the package name into full path name, such as * * -> D:/temp/a/com/dn/Demo.cla SS * * @param name * @return * private byte[] readFileToByteArray (String name) {InputStream is = null byte[]; rtnData = null; name = name.replaceAll ("conversion / / / /." "/"); / / filePath = this.path splicing String + name + ".Class"; File file = new File (filePath); ByteArrayOutputStream OS = new (ByteArrayOutputStream; try) {is = new FileInputStream (file); int TMP = 0; while ((TMP) = ()! = -1) {os.write} (TMP); rtnData = (os.toByteArray);} catch (Exception E) {e.printStackTrace} {(finally); If (null! = is) {{is.close} (try); catch (IOException E) {}} (e.printStackTrace); if (null! = OS) {{os.close} (try); catch (IOException E) {}}} (e.printStackTrace); return rtnData;} public Class< >? LoadClass (throws); ClassNotFoundException loadClass ( {return}};

(3) main method

Public class TestClassLoader {/ / to the full path name of the class of hot deployment, not necessarily in the project, can be in any position of the static private final String name = "jvm.dongnao.demo2.MessageImpl"; / / need hot deployment class classes private static final String directory path = "C://Users//Chen//workspace//effect-java//target//classes//"; public static void main (String[] args) throws Exception Message {message = null; while (true) {//1, instantiate its own class loader, and when the front side class loader as the parent class loader, and name and path in MyClassLoader loader MyClassLoader (Thread.currentThread) = new (.GetContextClassLoader), name (path, //2); Class<, call the loadClass method originally; > clazz = loader.loadClass (?); / / path+name+ ".Class" message instantiated above = (Message) (clazz.newInstance) (message.send); / / call method; / / every 3S to perform a check, class there is no change in Thread.sleep (3000);}}}

Three. The result
runs the main method directly, and then changes the output of the send method of MessageImpl to BBB to see the effect

Send message, AAAA send message, AAAA send message, BBBB send message, BBBB

Four, emphasize

The content of this chapter is a lot of code can be optimized, such as into byte, you can use NiO, close the stream, you can use Apache tool set, elegant shutdown, and so on.
, it’s not hard to see that I wrote a dead loop, and no matter what the class has changed, I’m going to reload it, that’s not good, you can use Apache VFS to listen to him.

The purpose of my writing is to illustrate the principles, not when the tools are used and the writing is so complex that it is not easy for people to understand. If you have any questions you can message can also contact me.

If interested, welcome to join the group, [Java beginners learning exchange group]: 458430385, this group has Java developers, UI designers and front-end engineers. Answer, study together, progress together!