java 动态代理

1.java 动态代理

实例:

package com.xp.dsp.dynamicProxy;

/**
 * Created by Skyline on 2016/6/16.
 */
public interface Subject {

    void doSomeThing();

}
 
package com.xp.dsp.dynamicProxy;

/**
 * Created by Skyline on 2016/6/16.
 */
public class SubjectImpl implements Subject {

    private String name = "subjectImpl";

    @Override
    public void doSomeThing() {
        System.out.println("run doSomeThing>>>>>>");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

package com.xp.dsp.dynamicProxy;

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

/**
 * 调用处理器
 * 该处理器定义代理的方法:执行被代理对象方法前后添加行为
 * Created by Skyline on 2016/6/16.
 */
public class ProxyHandler implements InvocationHandler {

    //被代理对象
    private Object obj;

    /**
     * 代理obj,返回代理对象
     * @param obj   被代理对象
     * @return
     */
    public Object proxy(Object obj) {
        this.obj = obj;
        /**
         * 生成代理对象
         * 该代理对象内部拥有该调用处理器ProxyHandler的实例:this
         */
        return Proxy.newProxyInstance(obj.getClass().getClassLoader(),obj.getClass().getInterfaces(),this);
    }

    /**
     * 代理对象执行方法时其实是委托给代理对象内部的InvocationHandler实例方法invoke
     * @param proxy     代理对象
     * @param method    执行的方法
     * @param args      执行的方法参数
     * @return
     * @throws Throwable
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        System.out.println("before calling: " + method.getName());
        method.invoke(obj, args);
        System.out.println("after calling: " + method.getName());
        return null;
    }
}

 
package com.xp.dsp.dynamicProxy;

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

/**
 * Created by Skyline on 2016/6/16.
 */
public class Test {

    public static void main(String[] args) {
        ProxyHandler proxyHandler = new ProxyHandler();
        Subject proxyInstance = (Subject)proxyHandler.proxy(new SubjectImpl());

        System.out.println("proxyInstance class name: " + proxyInstance.getClass().getName());

        Class<?>[] interfaces = proxyInstance.getClass().getInterfaces();
        for (Class<?> anInterface : interfaces) {
            System.out.println("interface:" + anInterface.getName());
        }

        Method[] methods = proxyInstance.getClass().getMethods();
        for (Method method : methods) {
            System.out.println("method:"+method.getName());method.getName();
        }

        Field[] fields = proxyInstance.getClass().getDeclaredFields();
        for (Field field : fields) {
            System.out.println("field:" + field.getName());
        }

        proxyInstance.doSomeThing();
        proxyInstance.toString();
        proxyInstance.equals("");
    }

}

执行结果:
proxyInstance class name: com.sun.proxy.$Proxy0
interface:com.xp.dsp.dynamicProxy.Subject
method:equals
method:toString
method:hashCode
method:doSomeThing
method:isProxyClass
method:getInvocationHandler
method:getProxyClass
method:newProxyInstance
method:wait
method:wait
method:wait
method:getClass
method:notify
method:notifyAll
field:m1
field:m2
field:m3
field:m0
before calling: doSomeThing
run doSomeThing>>>>>>
after calling: doSomeThing
before calling: toString
after calling: toString     
 
2. Cglib代理
 
	实例:
	
	
package com.xp.dsp.cglib;

/**
 * Created by Skyline on 2016/6/16.
 */
public class SubjectImpl{

    private String name = "subjectImpl";

    public void doSomeThing() {
        System.out.println("run doSomeThing>>>>>>");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
package com.xp.dsp.cglib;

//import org.springframework.cglib.proxy.MethodInterceptor;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * 采用cglib代理
 *
 * Created by Skyline on 2016/6/17.
 */
public class CustomCglib implements MethodInterceptor {

    //被代理对象
    private Object obj;

    /**
     * 代理obj,返回代理对象
     * @param obj 被代理对象
     * @return
     */
    public Object proxy(Object obj) {
        this.obj = obj;
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(this.obj.getClass());
        // 回调方法,即代理对象执行方法是会调用内部的回掉实例(CustomCglib的实例)的intercept方法。
        enhancer.setCallback(this);
        // 创建代理对象
        return enhancer.create();
    }


    @Override
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {

        System.out.println("before calling: " + method.getName());
        //调用
        methodProxy.invokeSuper(obj, args);
        System.out.println("after calling: " + method.getName());
        return null;
    }
}

package com.xp.dsp.cglib;

import java.lang.reflect.Method;

/**
 * Created by Skyline on 2016/6/16.
 */
public class Test {

    public static void main(String[] args) {
        CustomCglib cglib = new CustomCglib();
        SubjectImpl proxyInstance = (SubjectImpl)cglib.proxy(new SubjectImpl());

        System.out.println("proxyInstance class name: " + proxyInstance.getClass().getName());

        Class<?>[] interfaces = proxyInstance.getClass().getInterfaces();
        for (Class<?> anInterface : interfaces) {
            System.out.println("interface:" + anInterface.getName());
        }

        proxyInstance.doSomeThing();
        proxyInstance.toString();
    }

}
执行结果:
	proxyInstance class name: com.xp.dsp.cglib.SubjectImpl$$EnhancerByCGLIB$$edfb19c4
interface:net.sf.cglib.proxy.Factory
before calling: doSomeThing
run doSomeThing>>>>>>
after calling: doSomeThing
before calling: toString
Disconnected from the target VM, address: '127.0.0.1:54586', transport: 'socket'
before calling: hashCode
after calling: hashCode
after calling: toString
 
 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值