Spring源码学习之:ClassLoader学习(5)-自测

【一】测试目的(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包,否则会抛出异常

原文地址:https://www.cnblogs.com/shangxiaofei/p/6654801.html