《java中动态代理,springAop.pdf》由会员分享,可在线阅读,更多相关《java中动态代理,springAop.pdf(4页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、我也是刚学的,动态代理,我就我的理解来说一下它吧,看图,代码在最后面:package com;publicinterface Stu publicvoid add();publicvoid del();public String find();package com;publicclass Student implements Stu Overridepublicvoid add()System.out.println(you are run the add);Overridepublicvoid del()System.out.println(you are run the del);Ove
2、rridepublic String find()System.out.println(you are run the find);returni am from the method-finds return;package com;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;public class DynamicProxy implements InvocationHandler Object object;public
3、Object newclass(Object object)this.object=object;return Proxy.newProxyInstance(object.getClass().getClassLoader(),object.getClass().getInterfaces(),this);public Object invoke(Object proxy,Method method,Object args)throws Throwable this.chark();Object ret=null;ret=method.invoke(this.object,args);return ret;public void chark()System.out.println(!执行安检!);package com;publicclass Test /*param args*/publicstaticvoid main(String args)DynamicProxy daili=new DynamicProxy();Stu stu=(Stu)daili.newclass(new Student();/stu.add();System.out.println(stu.find();