码迷,mamicode.com
首页 > 其他好文 > 详细

修炼内功--动态代理

时间:2016-03-30 11:06:00      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

动态代理只能代理接口,不能代理某一个类。

动态代理包含面向切面编程,即在不操作原有代码的基础上可以操作原有代码。

动态代理实现步骤:创建代理类实现java.lang.reflect.InvocationHandler接口;创建目标对象,即需要代理的对象;完成Proxy.newProxyInstance()方法,传入的三个参数是难点我认为,第一个是目标对象的装载器(接口的装载器),第二个参数类,第三个参数为InvocationHandler对象。

写代理类有两种方法,但思想是一致的,先写一种。public interface HelloWorld{  String say();}

public class HelloWorldImpl{
  public String say(){
    System.out.println("hello world in proxyhelloworldimpl!");
    return "hello world!";
  }
}

/**
*代理类
*/
public class DynamicProxy implements InvocationHandler {
  
private Object target;
  
public Object newInstance(Object target){     this.target = target;     //这里的this代表的是invocationhandler对象,也就是DynamicProxy类     return Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getClassInterfaces(),this);   }   public Object invoke(Object proxy,Method method,Object[] args)throws Throwable{     System.out.println("before proxy");     //target:目标对象 args:传入的参数,比如调目标对象的那个方法     method.invoke(target,args);     System.out.println("before proxy");     return
null;   } }

 

 

写一个main类

public ProxyMain{
  public static void main(String[] args){
    DynamicProxy dp = new DynamicProxy();
    HelloWorld hw = new HelloWorldImpl()    HelloWorld hwp =(HelloWorld)dp.newInstance(hw);
    hwp.say();
  }
}

 

//执行结果

before proxy!

hello world in proxyhelloworldimpl!

after proxy!

 

还有一种写DynamicProxy类的形式是:


public DynamicProxy{

  private Object target;

  public DynamicProxy(Object target){
    this.target = target;
  }

  public Object invoke(Object proxy,Method method,Object[] args){
    System.out.println("before proxy");
    method.invoke(target,args);
    System.out.println("after proxy");
  }
}


/**
*都是利用Proxy.newProxyInstance方法完成代理对象
*/ public ProxyMain{   public void static main(String[] args){     HelloWorld hw = new HelloWorldImpl();     InvocationHandler dp = new DynamicProxy(hw);     HelloWorld hwi = (HelloWorld)Proxy.newProxyInstance(HelloWorld.class.getClassLoader,new Class<?>[]{HelloWorld.class},dp);     hwi.say();   } }
执行结果是和上一个代理类一样的结果。

修炼内功--动态代理

标签:

原文地址:http://www.cnblogs.com/bamaofan/p/5336221.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!