Java教程

Java动态代理3种实现方式

本文主要是介绍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;
    }

}
这篇关于Java动态代理3种实现方式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!