简述三种实现动态代理代码

package com.bosssoft.nontax.agency.invoice.servicecore;

import javassist.util.proxy.MethodHandler;
import javassist.util.proxy.ProxyFactory;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

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

/**
 * @desc 三种实现动态代理方式
 * @author ly
 * @date 2019-04-18
 */
public class ProxyTest {
    public static void main(String[] args) throws IllegalAccessException, InstantiationException {
        //jdk动态代理
        JdkProxy jdkProxy = (JdkProxy) Proxy.newProxyInstance(JdkProxy.class.getClassLoader(), Jdk.class.getInterfaces(), new JdkTest(new Jdk()));
        jdkProxy.printJdk();

        //cglib动态代理
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(CglibProxy.class);
        enhancer.setCallback(new CglibTest(new CglibProxy()));
        CglibProxy cglibProxy = (CglibProxy)enhancer.create();
        cglibProxy.printCglib();

        //javaassist动态代理
        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setSuperclass(Javaassist.class);
        proxyFactory.setHandler(new JavaassistTest(new Javaassist()));
        Javaassist javaassist = (Javaassist) proxyFactory.createClass().newInstance();
        javaassist.printJavaassist();
    }
}

interface JdkProxy{
    void printJdk();
}
class Jdk implements JdkProxy{

    @Override
    public void printJdk() {
        System.out.println("我是jdk");
    }
}

class JdkTest implements InvocationHandler{

    private Jdk jdk;
    public JdkTest(Jdk jdk){
        this.jdk = jdk;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("jdk before");
        method.invoke(jdk,args);
        System.out.println("jdk after");
        return proxy;
    }
}

class CglibProxy{
    void printCglib(){
        System.out.println("我是cglib");
    }
}

class CglibTest implements MethodInterceptor{

    private CglibProxy cglibProxy;

    public CglibTest(CglibProxy cglibProxy){
        this.cglibProxy = cglibProxy;
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("cglib before");
        method.invoke(cglibProxy,objects);
        System.out.println("cglib after");
        return o;
    }
}

class Javaassist{
    void printJavaassist(){
        System.out.println("我是javaassist");
    }
}

class JavaassistTest implements MethodHandler{

    private Javaassist javaassist;
    public JavaassistTest(Javaassist javaassist){
        this.javaassist = javaassist;
    }
    @Override
    public Object invoke(Object o, Method method, Method method1, Object[] objects) throws Throwable {
        System.out.println("javaassist before");
        method.invoke(javaassist,objects);
        System.out.println("javaassist after");
        return o;
    }
}

你可能感兴趣的:(Java)