Spring动态代理

JDK动态代理

  特点:代理目标对象必须有接口

  实质:内存中构建了接口的实现类

案例

创建ISomeService接口和实现类

//接口
package cn.happy.day06Proxy; /** * Created by Administrator on 2018/3/8. */ public interface ISomeService { public void doSome(); } //实现类 package cn.happy.day06Proxy; /** * Created by Administrator on 2018/3/8. */ public class SomeServiceImpl implements ISomeService { public void doSome() { System.out.println("do Something"); } }

  

Test测试类

package cn.happy.day06Proxy;

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

/**
 * Created by Administrator on 2018/3/8.
 */
public class Test {
    public static void main(String[] args) {
        
        final SomeServiceImpl service=new SomeServiceImpl();

        ISomeService proxy = (ISomeService)Proxy.newProxyInstance(service.getClass().getClassLoader(), service.getClass().getInterfaces(), new InvocationHandler() {
            
            /*
            * newProxyInstance(ClassLoader loader,   类加载器
                               Class<?>[] interfaces,  接口数组
                                          InvocationHandler h)  InvocationHandler对象
                                          */
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                
                /*
                * 
                * Object proxy,   代理对象
                 * Method method, 代理方法
                 * Object[] args  代理方法参数
                 * */
                System.out.println("Before-------");
                Object invoke = method.invoke(service, args);
                return invoke;
            }
        });

        proxy.doSome();
    }
}

  

结果:

CGLIB动态代理

  特点:在一个类没有接口的情况下进行代理

  实质:内存中构建了目标类型的子类

案例:

package cn.happy.day06Proxy;

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

import java.lang.reflect.Method;

/**
 * Created by Administrator on 2018/3/8.
 */
public class CglibTest {
    public static void main(String[] args) {
        final SomeServiceImpl service=new SomeServiceImpl();
        //Enhancer类是CGLib中的一个字节码增强器
        Enhancer enhancer=new Enhancer();

        enhancer.setSuperclass(service.getClass());
        //设置回调函数
        enhancer.setCallback(new MethodInterceptor() {
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                /*
               * o   代理对象
               *
               * method  代理目标方法
               *
               * objects 目标方法参数
               *
               * methodProxy  代理类的方法
               * */
                System.out.println("CGLIBbefore==========");
                Object invoke = methodProxy.invoke(service, objects);
                return invoke;
            }
        });

        SomeServiceImpl someService = (SomeServiceImpl)enhancer.create();
        someService.doSome();
    }
}

结果:

原文地址:https://www.cnblogs.com/xuchangqi1/p/8533445.html