An article to get to know CGlib dynamic proxies

Foreword

For the last article says the agency is moving JDK dynamic proxy using dynamic proxy to solve the static high coupling agent, code redundancy, easy maintenance and other issues, but the JDK dynamic proxy also has limitations, JDK dynamic proxy must have Interface , it can be dynamically reflected by proxy, if there is no interface how to do? Clang clang clang! ~ CGlib debut.

CGlib Features

  • For business class does not use interface, you can not use JDK dynamic proxies
  • CGlib with very bottom of the byte code technology, a subclass class, interface agent to solve the problem without

CGlib how to use

Since it does not require an abstract interface, and to engage in a direct instance of the class:

package com.bean.ReflectDemo2;

public class ProductDao {
    public void save() {
        System.out.println("保存用户...");
    }
    public void delete() {
        System.out.println("删除用户...");
    }
    public void find() {
        System.out.println("查找用户...");
    }
    public void update() {
        System.out.println("更新用户...");
    }
}

Four methods: CRUD, assuming that we need to find methods to enhance, permissions check before executing the find method, also known as AOP.
First, we need to write a proxy class: MyCglibProxy

package com.bean.ReflectDemo2;

import org.aopalliance.intercept.MethodInvocation;
import org.springframework.cglib.proxy.Callback;
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 MyCglibProxy implements MethodInterceptor {

    private ProductDao productDao;

    public MyCglibProxy(ProductDao productDao) {
        this.productDao = productDao;
    }

    public Object createProxy() {
        //1.创建核心类
        Enhancer enhancer = new Enhancer();
        //2.设置父类
        enhancer.setSuperclass(productDao.getClass());
        //3.设置回调
        enhancer.setCallback(this);
        //4.生成代理
        Object proxy = enhancer.create();
        return proxy;
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        if ("find".equals(method.getName())){
            System.out.println("权限校验=============");
            Object o1 = methodProxy.invokeSuper(o, objects);
            return o1;
        }
        return methodProxy.invokeSuper(o,objects);
    }
}

Can be seen, with the JDK dynamic proxy almost the same, but have different names, different individual call, it is quite easy to understand, and then look at the test class:

package com.bean.ReflectDemo2;

public class Demo {
    public static void main(String[] args) {
        ProductDao productDao = new ProductDao();
        ProductDao proxy = (ProductDao) new MyCglibProxy(productDao).createProxy();
        proxy.find();
    }
}

With JDK dynamic proxy almost the same, and more convenient.

to sum up

1, the program should give priority to create a proxy for the interface, ease of maintenance procedures decoupling
2, marked as final approach, the agent can not be, because they can not be covered

  • JDK dynamic proxy, an interface is generated for the sub-classes, interfaces final modification methods can not be used
  • CGlib target class is a subclass of production, and therefore class or method can not be used in the final
  • Spring supports only method of connecting point, does not support the properties of the connection point
Published 54 original articles · won praise 82 · views 90000 +

Guess you like

Origin blog.csdn.net/u011679785/article/details/99218116