Java动态代理3种实现方式

Java动态代理3种实现方式

一、被代理类

pulic interface ITarget {

    int sayHello();

    int sayBey();

}
public class Target implements ITarget {

    @Override
    public int sayHello() {
        System.out.println("hello");
        return 1;
    }

    @Override
    public int sayBey() {
        System.out.println("bye");
        return 2;
    }

}

二、静态代理

package com.yang.proxy;

/**
 * 静态代理
 */
public class StaticProxy {

    public static void main(String[] args) {
        StaticEnhance staticEnhance = new StaticEnhance(new Target());
        System.err.println(staticEnhance.sayHello());
        System.err.println(staticEnhance.sayBey());
    }

}

class StaticEnhance implements ITarget {

    private final ITarget target;

    public StaticEnhance(ITarget target) {
        this.target = target;
    }

    @Override
    public int sayHello() {
        System.out.println("===before hello");
        return this.target.sayHello() + 10;
    }

    @Override
    public int sayBey() {
        System.out.println("===before bey");
        return this.target.sayBey() + 100;
    }
}

三、JDK动态代理

package com.yang.proxy;

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

public class JdkDynamicProxy {

    public static void main(String[] args) {
        ITarget target = new Target();
        InvokeHandler handler = new InvokeHandler(target);
        ITarget proxyInstance = (ITarget) Proxy.newProxyInstance(
                                            target.getClass().getClassLoader(), 
                                            target.getClass().getInterfaces(),
                                            handler);
        proxyInstance.sayHello();
        proxyInstance.sayBey();
    }

}

class InvokeHandler implements InvocationHandler {

    private final Object target;

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

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.err.println("before " + method.getName());
        return method.invoke(this.target, args);
    }
}

四、cglib动态代理

package com.yang.proxy;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class CglibDynamicProxy {

    public static void main(String[] args) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(Target.class);
        enhancer.setCallback(new MyMethodProxy());

        Target target = (Target) enhancer.create();
        System.out.println(target.sayHello());
        System.out.println(target.sayBey());
    }

}

class MyMethodProxy implements MethodInterceptor {

    @Override
    public Object intercept(Object obj, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.err.println("before " + method.getName());
        Object result = methodProxy.invokeSuper(obj, objects);
        System.err.println("after " + method.getName());
        return result;
    }

}
上一篇:css样式选择器


下一篇:【雕爷学编程】Arduino动手做(98)---RC522 RFID射频模块