doraemongo 2009-04-14 11:49
浏览 237
已采纳

Java动态代理的疑问

小弟菜鸟,刚看动态代理,想请教诸位牛人一个问题,就是如何在InvocationHandler中代理多个方法?
比如有个接口叫Test,代码如下
[code="java"]public interface Test
{
void methodA();
void methodB();
void methodC(String a);
}[/code]
现在我有个实现类叫TestImpl,代码如下
[code="java"]
public class TestImpl implements Test
{
public void methodA()
{
Syste.out.println("methodA is invoked");
};
public void methodB()
{
Syste.out.println("methodBis invoked");
};
public void methodC(String s)
{
Syste.out.println("methodC receive parameter "+s);
};
}
[/code]
现在我希望有个TestImplProxy,它的作用是当调用TestImpl类中的MethodsA方法时,能够在[color=red]方法methodA运行前[/color]打印“I will run methodA”,能够在[color=red]方法methodB运行后[/color]打印“I have finished methodB”,能够在[color=red]方法methodC运行前[/color]打印“I will run methodC”和[color=red]运行后[/color]打印“I have finished methodC”
恳请熟悉的朋友能写个实例代码,谢谢您了

  • 写回答

2条回答 默认 最新

  • wanghaolovezlq 2009-04-14 12:03
    关注

    [code="java"]
    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;

    public class DynamicProxyTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Test test = new TestImpl();
        InvocationHandler handler = new TestHandler(test);
        Object proxy = Proxy.newProxyInstance(test.getClass().getClassLoader(),
                test.getClass().getInterfaces(), handler);
        Test proxyAfter = (Test)proxy;
        proxyAfter.methodA();
        proxyAfter.methodB();
        proxyAfter.methodC("aa");
    
    }
    

    }

    class TestHandler implements InvocationHandler
    {
    /**
    Constructs a TraceHandler
    @param t the implicit parameter of the method call
    */
    public TestHandler(Object t)
    {

    target = t;
    }

    public Object invoke(Object proxy, Method m, Object[] args) throws Throwable
    {

    if(m.getName().equals("methodA"))
    {
    System.out.println("I will run methodA");
    return m.invoke(target, args);
    }
    else if(m.getName().equals("methodB"))
    {
    System.out.println("I have finished methodB");
    return m.invoke(target, args);
    }
    else if(m.getName().equals("methodC"))
    {
    System.out.println("I will run methodC");
    Object o = m.invoke(target, args);
    System.out.println("I have finished methodC");
    return o;
    }

      return m.invoke(target, args);
    

    }

    private Object target;
    }

    interface Test
    {
    void methodA();
    void methodB();
    void methodC(String a);
    }

    class TestImpl implements Test
    {
    public void methodA()
    {
    System.out.println("methodA is invoked");
    };
    public void methodB()
    {
    System.out.println("methodBis invoked");
    };
    public void methodC(String s)
    {
    System.out.println("methodC receive parameter "+s);
    };
    }

    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码