自訂classloader裝載fastjson包,fastjson自訂註解

來源:互聯網
上載者:User

自訂classloader裝載fastjson包,fastjson自訂註解

public class Test {/** * @param args * @throws IllegalAccessException  * @throws InstantiationException  */public static void main(String[] args) throws InstantiationException, IllegalAccessException {ClassLoaderUtil.loadJarFile(new File("D:\\MyEclipse10\\unicomspace\\testclassload\\fastjson-1.1.26.jar"));Class<?> clazz = ClassLoaderUtil.loadclass("com.alibaba.fastjson.JSON");System.out.println(clazz.getSimpleName());}}


上面的代碼是測試,下面是實現

import java.io.File;  import java.lang.reflect.Method;  import java.net.URL;  import java.net.URLClassLoader;  import java.util.ArrayList;  import java.util.List;    /** 15. * @author obullxl 16. * 17. * email: obullxl@163.com  MSN: obullxl@hotmail.com  QQ: 303630027 18. * 19. * Blog: http://obullxl.iteye.com 20. */  public final class ClassLoaderUtil {      /** URLClassLoader的addURL方法 */  private static Method addURL = initAddMethod();            /** 初始化方法 */      private static final Method initAddMethod() {          try {              Method add = URLClassLoader.class                  .getDeclaredMethod("addURL", new Class[] { URL.class });              add.setAccessible(true);              return add;          } catch (Exception e) {              e.printStackTrace();          }          return null;      }        private static URLClassLoader system = (URLClassLoader) ClassLoader.getSystemClassLoader();        /**      * 迴圈遍曆目錄,找出所有的JAR包      */      private static final void loopFiles(File file, List<File> files) {          if (file.isDirectory()) {              File[] tmps = file.listFiles();              for (File tmp : tmps) {                  loopFiles(tmp, files);              }          } else {              if (file.getAbsolutePath().endsWith(".jar") || file.getAbsolutePath().endsWith(".zip")) {                  files.add(file);              }          }      }        /**      * <pre>      * 載入JAR檔案      * </pre>      *      * @param file      */      public static final void loadJarFile(File file) {          try {              addURL.invoke(system, new Object[] { file.toURI().toURL() });              System.out.println("載入JAR包:" + file.getAbsolutePath());          } catch (Exception e) {              e.printStackTrace();          }      }      public static final Class<?>  loadclass(String handler) {          try {           Class<?> clazz = system.loadClass(handler);         return clazz;        } catch (Exception e) {              e.printStackTrace();          }return null;      }      /**      * <pre>      * 從一個目錄載入所有JAR檔案      * </pre>      *      * @param path      */      public static final void loadJarPath(String path) {          List<File> files = new ArrayList<File>();          File lib = new File(path);          loopFiles(lib, files);          for (File file : files) {              loadJarFile(file);          }      }  }  

在很多情況是newinstance一個介面,這樣能動態裝載介面的實作類別

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.