Dynamic Class Loading Practice in Java: Solving Extensibility Challenges in List Management System
The article shows how to overcome extensibility limits in a Java list‑management system by creating a custom DynamicClassLoader that loads encrypted .class files at runtime, explains Java’s class‑loading stages, fixes a Tomcat deployment issue via proper parent delegation, and demonstrates dynamic invocation of new encryption modules without code changes.
This article discusses how to solve the extensibility problem in a list management system using Java custom class loaders. The system manages controlled applications on mobile devices, where different modules have different encryption methods for security. Traditional approaches using Template Method and Factory patterns require code modifications and version releases for new encryption methods.
The author proposes a solution using Java's class loading mechanism. Since the JVM specification does not strictly restrict how to obtain a class's binary byte stream, custom class loaders can dynamically load .class files, enabling code extensibility without modifying existing code.
The article covers the seven stages of class loading: Loading, Verification, Preparation, Resolution, Initialization, Using, and Unloading. It explains the three built-in class loaders: Bootstrap ClassLoader, Extension ClassLoader, and Application ClassLoader, and the parent delegation model.
A custom class loader DynamicClassLoader is implemented by extending ClassLoader and overriding the findClass method. The implementation reads encrypted .class files from disk and uses defineClass to convert byte arrays into Class objects.
A production issue is detailed: the custom class loader worked in local IDEA testing but failed on the Tomcat server with JSON parsing errors. The root cause was that when Base64 class (from hutool library) was loaded, it triggered the overridden findClass method, causing the JSON parsing logic to execute incorrectly.
The solution involves setting the parent class loader of DynamicClassLoader to be the class loader that loaded DynamicClassLoader itself (WebAppClassLoader in Tomcat), ensuring third-party classes are loaded through the proper delegation chain.
Key code implementation includes the custom class loader, the encrypt method in .class files, and the handler that uses reflection to invoke encryption methods dynamically.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
vivo Internet Technology
Sharing practical vivo Internet technology insights and salon events, plus the latest industry news and hot conferences.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
