首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > Java > 动态代理代码(体验拦截方法)
【标  题】:动态代理代码(体验拦截方法)
【关键字】:
【来  源】:http://www.blogjava.net/pear/archive/2006/06/10/51890.html

动态代理代码(体验拦截方法)

动态代理,作为实现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)是一样的

C# 3.0 新特性初步研究:【上一篇】
bba96 tiger 1.0 beta2 -- 源于bba96重构 降低侵入性,去除接口依赖:【下一篇】
【相关文章】
没有相关文章
【随机文章】
  • 得脱大难的故智
  • ASP错误的几种常规处理方式
  • Lisp
  • C语言编程常见问题解答之位(bit)和字节(byte)
  • params参数
  • 给你IE加上Google搜索工具
  • Coreldraw初始设置要项
  • IPV4与IPV6报头结构比较 表格精简篇
  • 河南省郑州远洋天地计算机网络工程有限公司--部分客户列表
  • 【分享】中国黑客界最新十大影响力人物
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.