42-44、类加载器
1】 Java虚拟机可以安装多个类加载器,系统默认的主要有三个,每个负责加载特定位置的类:
BootStrap、ExtClassLoader、AppClassLoader
2】 类加载器也是Java类,第一个加载器本身不是Java类,这正是BootStrap
3】 Java虚拟机中的所有类加载器采用具有父子关系的树形结构进行组织,在实例化每个类加载器对象时,需要为其指定一个父级加载器对象或默认采用系统加载器为其父级类加载。
4】 类加载器的委托机制
A、首先当前线程的类加载器去加载线程中的第一个类
如果类A中引用了B,java虚拟机将使用加载类A的类加载器来加载B
还可以直接调用ClassLoader.loadClass()方法来指定某个类加载器加载某个类
B、每个类加载器加载类时,又先委托其上级类加载器。
当所有祖宗类加载器没有加载到类,回到发起者类加载器,还加载不了,则抛出异常—ClassNotFoundException,不再去找发起者类加载器的儿子,因为没有getChild方法,即使有,那么多儿子,找哪一个呢?
45、编写自己的类加载器
自定义的类加载器必须继承ClassLoader
覆盖findClass方法
defineClass方法返回自定义类加载器的二进制代码
46、编写对class文件进行加密的工具类
需要加密的文件:
import
publicclassextends
public
return"hello ,itcast!"
}
" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" string="" destdir="args[1]; " p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" fileinputstream="" fis="new" fileinputstream="" srcpath="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" string="" destfilename="srcPath.substring(srcPath.lastIndexOf('//')+1); " p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" string="" destpath="destDir+'//'+destFileName; " p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" fileoutputstream="" fos="new" fileoutputstream="" destpath="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" cypher="" fis="" fos="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" fis="" close="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" fos="" close="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" span="" style="mso-tab-count: 2" font="" face="Times New Roman" size="2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" font="" span="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" nbsp="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 1" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" span="" style="mso-tab-count: 1" font="" face="Times New Roman" size="2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" font="" span="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 1" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" private="" static="" void="" cypher="" inputstream="" ips="" outputstream="" ops="" throws="" exception="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" int="" b="-1; " p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" while="" b="ips.read())!=-1){ " p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 3" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" ops="" write="" b="" 0xff="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 1" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" nbsp="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" size="2" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" nbsp="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" font="" size="2" span="" lang="EN-US" font="" face="Times New Roman" 47-48="" font="" span="" font="" style="mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'" face="宋体" font="" font="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" nbsp="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" font="" style="mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'" face="宋体" font="" size="2" font="" font="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" size="2" import="" java="" io="" bytearrayoutputstream="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" size="2" import="" java="" io="" fileinputstream="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" size="2" import="" java="" io="" filenotfoundexception="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" size="2" import="" java="" io="" fileoutputstream="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" size="2" import="" java="" io="" inputstream="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" size="2" import="" java="" io="" outputstream="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" nbsp="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" size="2" public="" class="" myclassloader="" extends="" classloader="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" nbsp="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 1" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 1" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" span="" style="mso-spacerun: yes" nbsp="" span="" param="" args="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 1" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" span="" style="mso-spacerun: yes" nbsp="" span="" throws="" filenotfoundexception="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 1" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" span="" style="mso-spacerun: yes" nbsp="" span="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 1" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" public="" static="" void="" main="" string="" args="" throws="" exception="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" nbsp="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" nbsp="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" string="" srcpath="args[0]; " p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" string="" destdir="args[1]; " p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" fileinputstream="" fis="new" fileinputstream="" srcpath="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" string="" destfilename="srcPath.substring(srcPath.lastIndexOf('//')+1); " p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" string="" destpath="destDir+'//'+destFileName; " p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" fileoutputstream="" fos="new" fileoutputstream="" destpath="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" cypher="" fis="" fos="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" fis="" close="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" fos="" close="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" span="" style="mso-tab-count: 2" font="" face="Times New Roman" size="2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" font="" span="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" nbsp="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 1" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" span="" style="mso-tab-count: 1" font="" face="Times New Roman" size="2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" font="" span="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 1" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" private="" static="" void="" cypher="" inputstream="" ips="" outputstream="" ops="" throws="" exception="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" int="" b="-1; " p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" while="" b="ips.read())!=-1){ " p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 3" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" ops="" write="" b="" 0xff="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 1" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" span="" style="mso-tab-count: 1" font="" face="Times New Roman" size="2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" font="" span="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 1" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" string="" classdir="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" span="" style="mso-tab-count: 1" font="" face="Times New Roman" size="2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" font="" span="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 1" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" override="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 1" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" protected="" class="" lt="" gt="" findclass="" string="" name="" throws="" classnotfoundexception="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" todo="" auto-generated="" method="" stub="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" span="" style="mso-tab-count: 2" font="" face="Times New Roman" size="2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" font="" span="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" span="" style="mso-tab-count: 2" font="" face="Times New Roman" size="2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" font="" span="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" string="" classfilename="classDir" name="" substring="" name="" lastindexof="" 1="" class="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" try="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 3" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" fileinputstream="" fis="new" fileinputstream="" classfilename="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 3" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" bytearrayoutputstream="" bos="new" bytearrayoutputstream="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 3" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" cypher="" fis="" bos="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 3" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" fis="" close="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 3" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" byte="" bytes="bos.toByteArray(); " p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 3" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" return="" defineclass="" bytes="" 0="" bytes="" length="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" span="" style="mso-tab-count: 3" font="" face="Times New Roman" size="2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" font="" span="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" catch="" exception="" e="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 3" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" todo="" auto-generated="" catch="" block="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 3" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" e="" printstacktrace="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" nbsp="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" return="" super="" findclass="" name="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 1" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" nbsp="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 1" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" public="" myclassloader="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" super="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 1" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" nbsp="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 1" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" public="" myclassloader="" string="" classdir="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 3" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" this="" classdir="classDir; " p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" span="" style="mso-tab-count: 1" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" span="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" span="" style="mso-tab-count: 1" font="" face="Times New Roman" size="2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" font="" span="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" span="" style="mso-tab-count: 1" font="" face="Times New Roman" size="2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" font="" span="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" span="" style="mso-tab-count: 1" font="" face="Times New Roman" size="2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" font="" span="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" span="" style="mso-tab-count: 1" font="" face="Times New Roman" size="2" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" nbsp="" font="" span="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" font="" size="2" nbsp="" font="" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" span="" lang="EN-US" font="" face="Times New Roman" size="2" font="" span="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt" font="" style="mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'" face="宋体" font="" size="2" font="" font="" p="" p="" class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-layout-grid-align: none" align="left" font="" face="">java.util.Date;
ClassLoaderAttachment Date {
@Override String toString() { ; }
运行类:
import
publicclass
main(String[] args) Exception {
new"itcastlib""classloader.ClassLoaderAttachment"
out
}
java.lang.reflect.Constructor;import
java.lang.reflect.Proxy;import
publicclass
main(String[] args) {
getProxyClass(Collection.classclass
out
for
new
'('
for
','
ifnulllength
')'
out
for
new
'('
for
','
ifnulllength
')'
out
Constructor constructor = clazzProxy1.getConstructor(InvocationHandler.class);
class MyInvocationHandler1 implements InvocationHandler{
@Override
return null;
}
proxy1.clear(); }
52-54: InvocationHandler对象的内部功能及其原理
1】Client程序调用代理方法时,涉及三要素:objProxy对象,方法,参数
2】代理类对象的继承自java.lang.Object的方法,仅hashCode,quals,toString进行派发给handler,其他方法自己实现
3】动态代理原理图
proxy;
java.lang.reflect.Constructor;import
java.lang.reflect.Method;import
java.util.ArrayList;import
publicclass
main(String[] args) Exception {
getProxyClass(Collection.classclass
out
for
new
'('
for
','
ifnulllength
')'
out
for
new
'('
for
','
ifnulllength
')'
out
out"--------------------"
class
classimplements
@Override Object invoke(Object proxy, Method method, Object[] args) Throwable { ; } } Collection proxy1=(Collection)constructor.newInstance( MyInvocationHandler1()); System..println(proxy1.toString()); proxy1.clear();
newProxyInstance(
class
newclass
new
ArrayList = ArrayList();
public
throws
longcurrentTimeMillis();
target
longcurrentTimeMillis();
out"running time:"
return
"lideguo"
"zahgnsd"
"tsdfesd"
"nidenia"
out
out
}package
import
java.lang.reflect.InvocationHandler;import
java.lang.reflect.Proxy;import
java.util.Collection;
ProxyTest {
publicstaticvoidthrows
Class clazzProxy1=Proxy.getProxyClass(Collection..getClassLoader(),Collection.); System..println(clazzProxy1.getName()); Constructor[] constructors=clazzProxy1.getConstructors(); (Constructor constructor: constructors){ String name=constructor.getName(); StringBuilder sBuilder= StringBuilder(name); sBuilder.append(); Class[] clazzParams = constructor.getParameterTypes(); (Class clazzParam : clazzParams){ sBuilder.append(clazzParam.getName()).append(); } (clazzParams!= &&clazzParams.!=0) sBuilder.deleteCharAt(sBuilder.length()-1); sBuilder.append(); System..println(sBuilder.toString()); } Method[] methods=clazzProxy1.getMethods(); (Method method: methods){ String name=method.getName(); StringBuilder sBuilder= StringBuilder(name); sBuilder.append(); Class[] clazzParams = method.getParameterTypes(); (Class clazzParam : clazzParams){ sBuilder.append(clazzParam.getName()).append(); } (clazzParams!= &&clazzParams.!=0) sBuilder.deleteCharAt(sBuilder.length()-1); sBuilder.append(); System..println(sBuilder.toString()); } System..println(); Constructor constructor = clazzProxy1.getConstructor(InvocationHandler.); MyInvocationHandler1 InvocationHandler{
publicthrows
returnnull
new
out
//proxy1.size(); ArrayList target= ArrayList();
new
"lideguo"
"zahgnsd"
"tsdfesd"
"nidenia"
out
out
Object getProxy( Object target, Advice advice) { Object proxy3=Proxy.newProxyInstance( target.getClass().getClassLoader(), target.getClass().getInterfaces(), InvocationHandler(){
@Override Object invoke(Object proxy, Method method, Object[] args) Throwable { advice.bfMethod(method); Object retVal=method.invoke(target, args); advice.afMethod(method); retVal; } } ); proxy3; }
56、实现类似spring的可配置的AOP框架
1】配置文件 config.properties
aop.ProxyFactoryBeancomeon.advice=
java.util.ArrayList
import
java.lang.reflect.*;
publicclass
Advice ; Object ; Advice getAdvice() { ; }
publicvoid
thisadvice
Object getTarget() { ; }
publicvoid
thistarget
Object getProxy() { TODO
target
target
new
public
throws
advice
target
advice
return
return
}
java.io.IOException;import
java.util.Properties;
BeanFactory {
propsnew
publicthrows
props
publicthrows
props
ifinstanceof
null
try
forName(props".advice"
forName(props".target"
catch
// Auto-generated catch block e.printStackTrace(); } proxy; } bean; }}import
java.util.Collection;
AopFrameworkTest {
/**
*@throwsException
*/ main(String[] args) Exception { TODO
InputStream ips=AopFrameworkTest..getResourceAsStream(); Object bean= BeanFactory(ips).getBean(); System..println(bean.getClass().getName()); ((Collection)bean).clear(); }