首页 > 编程语言 > 详细

java有关静态代理及动态代理的实现

时间:2018-07-25 19:46:53      阅读:133      评论:0      收藏:0      [点我收藏+]
public interface MyInterface {

    void doSomeThing();
    
    
    void someThingElse(String param);
}
public class RealObject implements MyInterface{

    @Override
    public void doSomeThing() {
        // TODO Auto-generated method stub
        print("开始调用doSomeThing方法");
        
    }

    @Override
    public void someThingElse(String param) {
        // TODO Auto-generated method stub
        print("开始调用somethingelse 方法;参数"+param);
        
    }

}
public class SimpleProxy implements MyInterface{
    
    private MyInterface myInterface;
    
    
    public SimpleProxy(MyInterface myInterface) {
        this.myInterface=myInterface;
    }

    @Override
    public void doSomeThing() {
        // TODO Auto-generated method stub
        print("业务逻辑dosomething...");
        myInterface.doSomeThing();
        
    }

    @Override
    public void someThingElse(String param) {
        // TODO Auto-generated method stub
        print("业务逻辑ELse...");
        myInterface.someThingElse(param);
        
    }
    
    public static void consumer(MyInterface myInterface) {
        
        myInterface.doSomeThing();
        myInterface.someThingElse("bubu..");
    }
    
    public static void main(String[] args) {
        consumer(new RealObject());
        consumer(new SimpleProxy(new RealObject()));
    }

}

以下是动态代理

 

自定义一个调用处理器:

/**
 * 调用处理器<P>
*  动态代理能把所有调用都重定向到调用处理器上
*/
public class DynamicProxyHandle implements InvocationHandler{

    private Object proxied;
    
    public DynamicProxyHandle(Object object) {
        this.proxied=object;
    }
    
    @Override
    public Object invoke(Object proxy,Method method,Object[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        print("proxy class:"+proxy.getClass()+" method:"+method+" args:"+args);
        
        if(method.getName().equals("doSomeThing")) {
            print("代理检测到doSomeThing被调用");
        }
//        if(args!=null) {
//            for(Object object:args) {
//                print("====="+object);
//            }
//        }
        
        
        
        return method.invoke(proxied, args);
    }

}
public class SimpleDynamicProxy {

    public static void consumer(MyInterface myInterface) {

        myInterface.doSomeThing();
        myInterface.someThingElse("bubu..");
    }

    public static void main(String[] args) {
        RealObject realObject = new RealObject();

        consumer(realObject);
        print("-----------------------------------------");
        /**
         * 1.一个类加载器【你通常可以从已经被加载的类对象中获取得到其类加载器,然后传递给它】
         * 2.希望该代理实现的接口列表【不是类或抽象类】
         * 3.以及InvocationHandler的一个实现(一个调用处理器)
         */
        MyInterface dynamicProxy = (MyInterface) Proxy.newProxyInstance(MyInterface.class.getClassLoader(),
                new Class[]{MyInterface.class}, new DynamicProxyHandle(realObject));
        consumer(dynamicProxy);

    }
}

 

java有关静态代理及动态代理的实现

原文:https://www.cnblogs.com/zhangfengshi/p/9367787.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!