java 动态代理

来源:互联网 发布:网络代理 免费 编辑:程序博客网 时间:2024/06/10 17:10

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.$Proxy0interface:com.xp.dsp.dynamicProxy.Subjectmethod:equalsmethod:toStringmethod:hashCodemethod:doSomeThingmethod:isProxyClassmethod:getInvocationHandlermethod:getProxyClassmethod:newProxyInstancemethod:waitmethod:waitmethod:waitmethod:getClassmethod:notifymethod:notifyAllfield:m1field:m2field:m3field:m0before calling: doSomeThingrun doSomeThing>>>>>>after calling: doSomeThingbefore calling: toStringafter 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$$edfb19c4interface:net.sf.cglib.proxy.Factorybefore calling: doSomeThingrun doSomeThing>>>>>>after calling: doSomeThingbefore calling: toStringDisconnected from the target VM, address: '127.0.0.1:54586', transport: 'socket'before calling: hashCodeafter calling: hashCodeafter calling: toString

0 0
原创粉丝点击