Java基础加强---动态代理

   -------android培训java培训、期待与您交流! ----------

1.动态代理

      动态代理类:JVM可以在运行期动态生成出类的字节码,这种动态生成的类往往被用作代理类。

JVM生成的动态类必须实现一个或多个接口,所以JVM生成的动态类只能用作具有相同接口的目标类的代理。CGLIB库可以动态生成一个类的子类,一个类的子类也可以用作该类的代理,所以如果要为一个没有实现接口的类生成动态代理类,可以使用CGLIB库。

2.代理的使用

代理类的各个方法中通常除了要调用目标的相应方法和对外返回目标返回的结果外,还可以在代理方法中的如下四个位置加上系统功能代码:

(1)在调用目标方法之前;

(2)在调用目标方法之后;

(3)在调用目标方法前后;

(4)在处理目标方法异常的catch块中。

 

3.生成动态代理类实例对象

   (1)class MyInvocationHandler implements InvocationHandler{

         @Override
             public Object invoke(Object proxy, Method method, Object[] args)
                     throws Throwable {

                         return null;

                        }

          }

       Collection proxy1 = (Collection)constructor.newInstance(new MyInvocationHandler());

        

   (2)//用匿名类实现 InvocationHandler

       Collection proxy2 = (Collection)constructor.newInstance(new InvocationHandler(){
           public Object invoke(Object proxy, Method method, Object[] args)
                throws Throwable {

                    return null;

                   }
                });  

(3)Collection proxy3 = (Collection)Proxy.newProxyInstance

                                     (Collection.class.getClassLoader(),

                                         new Class[]{Collection.class},

                                         new MyInvocationHandler(){
                                              public Object invoke(Object proxy, Method method,

                                                  Object[] args) throws Throwable {

                                                                                                                          return null;

                                                                                                                      }
                                                                                                                });

4.动态代理演示

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

//定义一个接口

interface Person{
 void walk();
 void sayHello(String name);
}

//实现InvocationHandler接口,并重写invoke方法
class MyInvocationHandler implements InvocationHandler{

 @Override
 public Object invoke(Object proxy, Method method, Object[] args)
   throws Throwable {
  // TODO Auto-generated method stub
  System.out.println("正在执行的方法:"+method);
  if(args!=null)
   {
    System.out.println("下面是执行该方法时传入的参数:");
    for(Object val:args)
     System.out.println(val);
   } else {
    System.out.println("该方法没有参数。。。");
   }
  return null;
 }
 
}
public class ProxyTest1 {
   public static void main(String[] args){
    InvocationHandler handler = new MyInvocationHandler();

   //创建代理对象
    Person p = (Person) Proxy.newProxyInstance
      (Person.class.getClassLoader(),
    new Class[]{Person.class},
    handler);

   //用代理对象调用方法
    p.walk();
    p.sayHello("卡卡罗特");
   }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值