Using dynamic proxy CGLIB

Reference: https: //blog.csdn.net/yhl_jxy/article/details/80633194#comments

CGLIB dynamic proxy

Definitions: CGLIB (code genaration libary), code generation library.

Automatically generate an extended class interface and implementation java program run, is a powerful, high-performance code generation packet

CGLIB dynamic proxy implementation

1) introducing the desired cglib jar package

DETAILED logic 2), to achieve MethodIntercetor class, proxy class object implement

       重写intercept()方法,在intercept方法中实现代理的逻辑

      intercept(Object o, Method method, Object[] Objects, MehodProxy methodProxy )

       o: 被代理类对象

        method: 被代理类的方法

    objects: 被代理类方法的参数

     methodProxy: 调用被代理类对象的参数

3) using Enhance generate proxy class object

          enhance.create() //生成代理类对象

Use CGLIB implement lazy loading

MehodInterceptor implementation class:

/**
 * 实现代理的逻辑
 * 步骤:
 *   --: 1、实现MethodInterceptor拦截器
 *        o: 被代理对象
 *        method: 被代理对象的方法
 *        objects: 被代理对象的方法参数
 *        methodProxy: 触发父类方法的对象
 */
public class DBQueryCglibMethodInterceptor implements MethodInterceptor {
    DBQuery dbQuery = null;
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
           if(dbQuery == null){
               dbQuery = new DBQuery();
           }
           return dbQuery.Request();
    }
}

Real categories:

/**
 * 使用cglib来实现数据库的查询
 */
public class DBQuery {
    public String Request(){
        return "数据库查询完毕";
    }
}

Use categories:

public class Client {
    public static void main(String[] args) {
        /**
         * 创建增强类对象
         */
        Enhancer enhancer = new Enhancer();

        /**
         * 创建增强类的父类对象
         */
        enhancer.setSuperclass(DBQuery.class);

        /**
         * 创建拦截切入类
         */
        enhancer.setCallback(new DBQueryCglibMethodInterceptor());

        /**
         * 创建代理对象
         */
        DBQuery dbQueryProxy = (DBQuery) enhancer.create();
        String result = dbQueryProxy.Request();
        System.out.println(result);
    }
}

result:

数据库查询完毕

CGLIB use enhanced features of the original class

MethodIntercptor implementation class

/**
 * cglib代理逻辑切入类
 *  ---: intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy)
 *       o: 被代理类对象
 *       method: 要拦截的被代理类的方法
 *       objects:被代理类的方法参数
 *       methodyProxy: 要触发父类的方法对象
 */
public class CglibMethodInteceptor implements MethodInterceptor {
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("插入前置通知");
        Object object = methodProxy.invokeSuper(o, objects);
        System.out.println("插入后置通知");
        return object;
    }
}

Real categories:

/**
 * 使用cglib实现动态代理,不用创建接口
 */
public class HelloService {
    HelloService(){
        System.out.println("HelloServie构造器");
    }

    public final String sayOthers(String name){
        System.out.println("HelloService: sayOthers" +name);
        return null;
    }

    public void sayHello(){
        System.out.println("HelloService: sayHello");
    }

    public void sayByeBye(){
        System.out.println("HelloService: sayByeBye");
    }
}

Use categories:

public class Client {
    public static void main(String[] args) {
        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "D:\\code");
        /**
         * 创建增强类对象
         */
        Enhancer enhancer = new Enhancer();

        /**
         * 创建需要增强的父类对象
         */
        enhancer.setSuperclass(HelloService.class);

        /**
         * 设置切入类对象
         */
        enhancer.setCallback(new CglibMethodInteceptor());

        /**
         * 生成代理对象
         */

        HelloService service = (HelloService) enhancer.create();
        service.sayHello();
        service.sayByeBye();
    }
}

result:

HelloServie构造器
插入前置通知
HelloService: sayHello
插入后置通知
插入前置通知
HelloService: sayByeBye
插入后置通知

Guess you like

Origin www.cnblogs.com/Auge/p/11579283.html