Spring原始碼學習之:ClassLoader學習(5)-自測

Love Lenka發表於2017-04-01

【一】測試目的(ClassLoader的作用)

1:測試涉及三個jar包,nonbankcard-configure-0.0.1-SNAPSHOT.jar,nonbankcard-persist-0.0.1-SNAPSHOT.jar,fastjson-1.2.8.sec01.jar

2:將這三個jar包放在指定的目錄裡(/usr/sxf/testcls)

3:在專案中編輯類載入的jar,載入到記憶體中,執行jar包中的方法

 

【二】測試程式碼

1:nonbankcard-persist-0.0.1-SNAPSHOT.jar中的程式碼

 1 package org.nonbankcard.persist;
 2 /**
 3  * 該類會在nonbankcard-configure-0.0.1-SNAPSHOT.jar類中被引用
 4  * @author sxf
 5  *
 6  */
 7 public class SxfApp {
 8     private String name;
 9     private String age;
10     private String dos;
11     public String getName() {
12         return name;
13     }
14     public void setName(String name) {
15         this.name = name;
16     }
17     public String getAge() {
18         return age;
19     }
20     public void setAge(String age) {
21         this.age = age;
22     }
23     public String getDos() {
24         return dos;
25     }
26     public void setDos(String dos) {
27         this.dos = dos;
28     }
29     
30 }
View Code

2:nonbankcard-configure-0.0.1-SNAPSHOT.jar 中的程式碼

 1 package com.nonbank.sxf.test.cls;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import org.nonbankcard.persist.SxfApp;
 7 
 8 import com.alibaba.fastjson.JSON;
 9 /**
10  * 該類中的程式碼引用了
11  * =>nonbankcard-persist-0.0.1-SNAPSHOT.jar中的程式碼
12  * =>fastjson-1.2.8.sec01.jar中的程式碼
13  * =>本jar包中的User類
14  * @author sxf
15  *
16  */
17 public class SxfTestUtils {
18 
19     /**
20      * 靜態方法
21      * @param list
22      * @return
23      */
24     public static String formatList(List<String> list){
25         List<User> userList=new ArrayList<User>();
26         List<SxfApp> apps=new ArrayList<SxfApp>();
27         for(String str:list){
28             User u=JSON.parseObject(str, User.class);
29             userList.add(u);
30         }
31         for(User usr:userList){
32             System.out.println("SxfTestUtils.formatList()"+usr.getName());
33             System.out.println("SxfTestUtils.formatList()"+usr.getAge());
34             System.out.println("SxfTestUtils.formatList()"+usr.getDos());
35             SxfApp sxfApp=new SxfApp();
36             sxfApp.setName(usr.getName());
37             sxfApp.setAge(usr.getAge());
38             sxfApp.setDos(usr.getDos());
39             apps.add(sxfApp);
40         }
41         
42         StringBuilder builder=new StringBuilder();
43         for(SxfApp app:apps){
44             builder.append("@").append("姓名==>[").append(app.getName()).append("]年齡==>[").append(app.getAge()).append("]");
45             builder.append("喜歡做的事情==>[").append(app.getDos()).append("]");
46         }
47         return builder.toString().substring(1);
48     }
49     
50     /**
51      * 非靜態方法
52      * @param list
53      * @return
54      */
55     public String formatEx(List<String> list){
56         List<User> userList=new ArrayList<User>();
57         List<SxfApp> apps=new ArrayList<SxfApp>();
58         for(String str:list){
59             User u=JSON.parseObject(str, User.class);
60             userList.add(u);
61         }
62         for(User usr:userList){
63             System.out.println("SxfTestUtils.formatList()"+usr.getName());
64             System.out.println("SxfTestUtils.formatList()"+usr.getAge());
65             System.out.println("SxfTestUtils.formatList()"+usr.getDos());
66             SxfApp sxfApp=new SxfApp();
67             sxfApp.setName(usr.getName());
68             sxfApp.setAge(usr.getAge());
69             sxfApp.setDos(usr.getDos());
70             apps.add(sxfApp);
71         }
72         
73         StringBuilder builder=new StringBuilder();
74         for(SxfApp app:apps){
75             builder.append("@").append("姓名==>[").append(app.getName()).append("]年齡==>[").append(app.getAge()).append("]");
76             builder.append("喜歡做的事情==>[").append(app.getDos()).append("]");
77         }
78         return builder.toString().substring(1);
79     }
80     
81     /**
82      * 非靜態方法
83      * @param str
84      * @return
85      */
86     public String sxf(String str){
87         System.out.println("SxfTestUtils.sxf(aaaaaaaaaaaaaaaaaaaaaaaaaaaa)");
88         return "記載======>"+str;
89     }
90 }
View Code

3:測試類

  1 package org.nonbankcard.commons;
  2 
  3 import java.io.File;
  4 import java.io.FileFilter;
  5 import java.lang.reflect.Constructor;
  6 import java.lang.reflect.InvocationTargetException;
  7 import java.lang.reflect.Method;
  8 import java.net.MalformedURLException;
  9 import java.net.URL;
 10 import java.net.URLClassLoader;
 11 import java.util.ArrayList;
 12 import java.util.List;
 13 
 14 import org.mvel2.util.ThisLiteral;
 15 /**
 16  * 
 17  * @author sxf
 18  *
 19  */
 20 public class SxfTestClass {
 21     
 22     private final String CLASS_NAME="com.nonbank.sxf.test.cls.SxfTestUtils";
 23     //工具類對應的物件
 24     private Object object=null;
 25     //格式化的方法物件,靜態。List作為形參
 26     private Method formatListMethod=null;
 27     //非靜態的方法物件。List作為形參
 28     private Method formatExMethod=null;
 29     //非靜態的方法物件。String作為形參
 30     private Method sxfMehod=null;
 31     
 32     
 33     
 34     
 35     /**
 36      * 建構函式中,載入指定路徑的jar包。呼叫jar包中的方法
 37      * @throws MalformedURLException
 38      * @throws ClassNotFoundException
 39      * @throws NoSuchMethodException
 40      * @throws SecurityException
 41      * @throws InstantiationException
 42      * @throws IllegalAccessException
 43      * @throws IllegalArgumentException
 44      * @throws InvocationTargetException
 45      */
 46     public SxfTestClass() throws MalformedURLException, ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
 47         //jar包所在目錄
 48         File file=new File("/usr/sxf/testcls");
 49         
 50         //載入目錄下所有的jar檔案
 51         File[] files=file.listFiles(new FileFilter() {
 52             public boolean accept(File pathname) {
 53                 String name = pathname.getName().toLowerCase();   
 54                 System.out.println("SxfTestClass.enclosing_method()"+name);
 55                  return name.endsWith("jar");   
 56             }
 57         });
 58         //classLoarder載入
 59         URL[] urls = new URL[files.length];     
 60          for(int i = 0; i < files.length; i++) {   
 61               urls[i] = new URL("file",null,files[i].getAbsolutePath());   
 62          }
 63          //將jar包全部載入到classLoader中
 64          ClassLoader classLoader = new URLClassLoader(urls, null);
 65          
 66          //反射生成jar包中的類的物件,和要執行方法的物件
 67          Class cls=classLoader.loadClass(CLASS_NAME);
 68          //建構函式
 69          Constructor constructor    = cls.getConstructor(new Class[]{});
 70          //反射生成物件
 71          this.object=constructor.newInstance(new Object[]{});
 72          //反射生成要執行方法的物件
 73          this.formatListMethod=cls.getDeclaredMethod("formatList",new Class[] {List.class});
 74          this.formatExMethod=cls.getDeclaredMethod("formatEx", new Class[] {List.class});
 75          this.sxfMehod=cls.getDeclaredMethod("sxf",  new Class[] {String.class});
 76          
 77     }
 78 
 79     public static void main(String[] args) throws MalformedURLException, ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
 80         SxfTestClass sxfTestClass=new SxfTestClass();
 81         sxfTestClass.testclassLoader();
 82     }
 83     
 84     /**
 85      * 靜態,非靜態的方法都可被執行
 86      * @throws IllegalAccessException
 87      * @throws IllegalArgumentException
 88      * @throws InvocationTargetException
 89      * 
 90      */
 91     public void testclassLoader() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException{
 92         String a="{'name':'ddd','age':'28','dos':'學些寫java程式碼'}";
 93         String b="{'name':'eee','age':'28','dos':'打籃球'}";
 94         List<String> list=new ArrayList<String>();
 95         list.add(a);
 96         list.add(b);
 97         Object strObject=formatListMethod.invoke(object, list);
 98         System.out.println("SxfTestClass.testclassLoader()"+strObject);
 99         
100         System.out.println("================================");
101         
102         Object strObject2=formatExMethod.invoke(this.object, list);
103         System.out.println("SxfTestClass.testclassLoader()"+strObject2);
104         
105         System.out.println("================================");
106         
107         Object object=sxfMehod.invoke(this.object, "東");
108         System.out.println("SxfTestClass.testclassLoader()"+object);
109         
110     }
111     
112     
113     /**
114      * 
115 測試結果:
116 SxfTestClass.enclosing_method()nonbankcard-configure-0.0.1-snapshot.jar
117 SxfTestClass.enclosing_method()nonbankcard-persist-0.0.1-snapshot.jar
118 SxfTestClass.enclosing_method()fastjson-1.2.8.sec01.jar
119 SxfTestUtils.formatList()ddd
120 SxfTestUtils.formatList()28
121 SxfTestUtils.formatList()學些寫java程式碼
122 SxfTestUtils.formatList()eee
123 SxfTestUtils.formatList()28
124 SxfTestUtils.formatList()打籃球
125 SxfTestClass.testclassLoader()姓名==>[ddd]年齡==>[28]喜歡做的事情==>[學些寫java程式碼]@姓名==>[eee]年齡==>[28]喜歡做的事情==>[打籃球]
126 ================================
127 SxfTestUtils.formatList()ddd
128 SxfTestUtils.formatList()28
129 SxfTestUtils.formatList()學些寫java程式碼
130 SxfTestUtils.formatList()eee
131 SxfTestUtils.formatList()28
132 SxfTestUtils.formatList()打籃球
133 SxfTestClass.testclassLoader()姓名==>[ddd]年齡==>[28]喜歡做的事情==>[學些寫java程式碼]@姓名==>[eee]年齡==>[28]喜歡做的事情==>[打籃球]
134 ================================
135 SxfTestUtils.sxf(aaaaaaaaaaaaaaaaaaaaaaaaaaaa)
136 SxfTestClass.testclassLoader()記載======>東
137 
138      */
139         
140     
141 }
View Code

 

【三】測試結果

1:執行目錄必須存在,要呼叫方法依賴的所有jar包,否則會丟擲異常

相關文章