本站首页    管理页面    写新日志    退出


«September 2025»
123456
78910111213
14151617181920
21222324252627
282930


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7618730
建立时间:2006年5月29日




[J2SE]动态代理代码(体验拦截方法)
软件技术,  电脑与网络

lhwork 发表于 2006/6/12 14:42:07

动态代理,作为实现AOP的方式之一,已经得到广泛的应用.本人看了很多书关于动态代理的介绍,基本就是不知所云. 所以最终自己做了一个例子,才感到有点明白,下面是我的代码package pear;   //import org.springframework.aop.Advisor; //import org.springframework.aop.BeforeAdvice; import java.lang.reflect.*;   interface SayInterface{          // 被代理借口        public void say();        public void saytwo(); } class Say implements SayInterface{    // 被代理的类        public void say(){               System.out.println(" in say ");        }        public void saytwo(){               System.out.println("in say 2");        } }   class SayHandler implements InvocationHandler{     //        private Say mysay = new Say();         //先生成一个"被代理类"的对象        public Object invoke(Object proxy,Method method,Object[] args)throws Exception{               System.out.println("Before");    //方法调用之前输出Before            method.invoke(mysay,args);         //原来方法在这里才真正被调用               //method.invoke(saytwo,args);               System.out.println("After");      //方法调用之后输出After               return null;        } }   public class Main {  /**  * @param args  */ public static void main(String[] args) {  // TODO Auto-generated method stub  SayHandler handler = new SayHandler();  SayInterface si = (SayInterface)Proxy.newProxyInstance(    SayInterface.class.getClassLoader(),new Class[]{SayInterface.class},handler);/**  SayInterface si = (SayInterface)Proxy.newProxyInstance(//       handler.getClass().getClassLoader(),handler.say.getClass().getInterfaces(),handler);  //被代理接口的类加载器,被加载的类,和。。。。        SayInterface si = (SayInterface)Proxy.newProxyInstance(handler.getClass().getClassLoader(),);   */    si.say();  si.saytwo();  System.out.println("the end.........."); } }//说明同一个包里面的类的类加载器(ClassLoader)是一样的


阅读全文(1820) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.047 second(s), page refreshed 144796460 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号