[Dark Horse Programmer Jinan Center] Proxy Mode - Cglib Proxy

[Dark Horse Programmer Jinan Center] Proxy Mode - Cglib Proxy
Today , I will talk to you about the third design mode of the proxy mode in java - Cglib mode. We talked about static proxy and dynamic proxy two days ago, but have you noticed that both proxy modes require the target object to be a target object that implements an interface, but sometimes the target object is just a separate object and does not implement any Interface. At this time we can use the Cglib mode, which can use the class to implement the proxy in the form of a subclass of the target object.
The concept of Cglib proxy: also known as subclass proxy, it builds a subclass object in memory to extend the function of the target object.
JDK dynamic proxy has a limitation, that is, the object using dynamic proxy must implement one or more Interface, if you want to proxy a class that does not implement an interface, you can use Cglib to implement it.
Cglib is a powerful high-performance code generation package, which can extend java classes and implement java interfaces at runtime. It is widely used by many AOP frameworks Using, for example, Spring AOP and synaop, the interception (interception)
Cglib package provides methods for them. The bottom layer of the Cglib package is to convert the bytecode and generate new classes by using a small and block bytecode processing framework ASM. Direct use is discouraged ASM, because it requires you to be familiar with the internal structure of the JVM, including the format and instruction set of the class file.
Cglib subclass proxy implementation method:
1. The jar file of cglib needs to be imported, but the Cglib function is already included in the core package of Spring , so you can directly import pring-core-3.2.5.jar.
2. After the function package is introduced, you can dynamically build subclasses in memory
3. The proxy class cannot be final, otherwise an error will be reported
4. If the method of the target object If it is final/static, it will not be intercepted, that is, no additional business methods of the target object will be executed.
The following is an example of using Cglib proxy:
target object class: UserDao.java:
/**
* target object, does not implement any interface
*/
public class UserDao {

public void save() {
    System.out.println("----已经保存数据!----");
}

}

Cglib proxy factory: ProxyFactory.java:
/**
* Cglib subclass proxy factory
* Dynamically build a subclass object in memory for UserDao
*/
public class ProxyFactory implements MethodInterceptor{
//Maintain the target object
private Object target;

public ProxyFactory(Object target) {
    this.target = target;
}

//给目标对象创建一个代理对象
public Object getProxyInstance(){
    //1.工具类
    Enhancer en = new Enhancer();
    //2.设置父类
    en.setSuperclass(target.getClass());
    //3.设置回调函数
    en.setCallback(this);
    //4.创建子类(代理对象)
    return en.create();

}

@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
    System.out.println("开始事务...");

    //执行目标对象的方法
    Object returnValue = method.invoke(target, args);

    System.out.println("提交事务...");

    return returnValue;
}

}
Test class:
/**
* Test class
*/
public class App {

@Test
public void test(){
    //目标对象
    UserDao target = new UserDao();

    //代理对象
    UserDao proxy = (UserDao)new ProxyFactory(target).getProxyInstance();

    //执行代理对象的方法
    proxy.save();
}

}

In Spring's AOP programming, if the target object added to the container has an implementation interface, JDK is used as a proxy. If the target object does not implement an interface, Cglib is used as a proxy.
Generally, the Cglib proxy is used in the Spring framework, so friends can take a look at it as an understanding of the content.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324726656&siteId=291194637