博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring源码学习之:ClassLoader学习(5)-自测
阅读量:7072 次
发布时间:2019-06-28

本文共 9378 字,大约阅读时间需要 31 分钟。

【一】测试目的(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 sxf15  *16  */17 public class SxfTestUtils {18 19     /**20      * 静态方法21      * @param list22      * @return23      */24     public static String formatList(List
list){25 List
userList=new ArrayList
();26 List
apps=new ArrayList
();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 list53 * @return54 */55 public String formatEx(List
list){56 List
userList=new ArrayList
();57 List
apps=new ArrayList
();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 str84 * @return85 */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
list=new ArrayList
(); 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.jar117 SxfTestClass.enclosing_method()nonbankcard-persist-0.0.1-snapshot.jar118 SxfTestClass.enclosing_method()fastjson-1.2.8.sec01.jar119 SxfTestUtils.formatList()ddd120 SxfTestUtils.formatList()28121 SxfTestUtils.formatList()学些写java代码122 SxfTestUtils.formatList()eee123 SxfTestUtils.formatList()28124 SxfTestUtils.formatList()打篮球125 SxfTestClass.testclassLoader()姓名==>[ddd]年龄==>[28]喜欢做的事情==>[学些写java代码]@姓名==>[eee]年龄==>[28]喜欢做的事情==>[打篮球]126 ================================127 SxfTestUtils.formatList()ddd128 SxfTestUtils.formatList()28129 SxfTestUtils.formatList()学些写java代码130 SxfTestUtils.formatList()eee131 SxfTestUtils.formatList()28132 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包,否则会抛出异常

转载地址:http://fxell.baihongyu.com/

你可能感兴趣的文章
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
查看>>
AIDL 基础知识 知识点总结
查看>>
ORA-04028: cannot generate diana for object xxx
查看>>
Java 7之传统I/O - 字符类 StringReader和StringWriter
查看>>
STL Set和multiset 容器
查看>>
js里size和length的区别
查看>>
《简明Python教程》读书笔记
查看>>
关于大衍求一术的一个延拓
查看>>
Centos7.2 安装Elasticsearch 6
查看>>
注册Azure AD 2.0 应用程序
查看>>
向大神学习
查看>>
Python 字符串常用方法
查看>>
Android Could not find com.afollestad:material-dialogs:0.7.6.0 解决
查看>>
构造定律(constructal law)-构造定律作为第二个时间箭头,将和热力学第二定律一道将宇宙推向无序。...
查看>>
八大免费SSL证书-给你的网站免费添加Https安全加密
查看>>
虚拟机安装苹果系统 出现不可恢复的错误解决办法
查看>>
Cocos2d中的Menu使用
查看>>
PHP中include()与require()的区别说明
查看>>
Mybatis之基于XML的调用存储过程与手动回滚事务
查看>>
csrf攻击
查看>>