The difference between JDK dynamic proxy and CGLIB dynamic proxy

Mondo Technology Updated on 2024-02-01

#The difference between JDK dynamics and CGLIB dynamics

There are two main ways to be dynamic in Spring AOP, JDK dynamic and CGLIB dynamic

JDK dynamic only provides interfaces, and does not support classes.

1) The JDK will generate a dynamic class $proxy*. at runtimeclass .

2) The class implements the interface of the target class, and the class implements all the method enhancements of the interface.

3) When invoking, the ** class first calls the processing class for enhancement, and then calls the target method through reflection. Thus achieving AOP

If the class doesn't implement an interface, then Spring AOP chooses to use CGLIB to dynamically target the class.

1) The underlying layer of CGLIB is to dynamically generate a subclass of the target class at runtime via ASM. (There are other related classes, mainly to enhance efficiency when calling) that generate multiple ,

2) and will override all the methods of the parent class to enhance **, 3) when calling, first enhance by **class, and then directly call the corresponding method of the parent class to call the target method. Thus achieving AOP

CGLIB is dynamic by inheritance, so if a class is marked as final, it can't be animated using CGLIB.

In addition to generating the target subclass, CGLIB also has a fastclass (routing class) that can (but does not have to) be augmented by method calls of this class, without the invalidation of method call enhancements like JDK.

Many people will compare the performance of JDK and CGLIB, JDK dynamically generates classes quickly and calls slowly, and CGLIB generates classes slowly, but subsequent calls.

Fast, in the old version of CGLIB is about 10 times faster than JDK, but in fact, the speed of JDK is much higher every time the version is upgraded, and.

CGLIB is still faltering.

In the experiments of JDK dynamic ** and CGLIB dynamic **, the dynamic ** performance of JDK7 and 8 is about 20% better than that of CGLIB under 1W execution.

Additional Extensions.

1. What's the use of dynamics**

He can create objects.

In the case of the original ** unchanged and unchanged, the original functions are enhanced (a bit like the decorator mode).

Decoupling allows you to separate business functions from logs.

Second, two ways to implement **

1) JDK dynamics**:

This is achieved through these three classes: proxy, method, and invocationhandler.

It is required that the target class must implement the interface.

2), CGLIB Dynamics**.

The principle is inheritance.

Enhance the target class method by creating a subclass to override the parent class's methods.

3. JDK Dynamics** Demo**

Development steps: Create the target class interface someservice

j**apackage com.mr.lee.service;

public interface someservice

override

public string doother()

Create an implementation class for the InvocationHandler interface that implements enhancements to the target method.

j**apackage com.mr.lee.dao;

import j**a.lang.reflect.invocationhandler;

import j**a.lang.reflect.method;

Implement the InvocationHandler interface to enhance functionality to the target method.

public class myinvocationhandler implements invocationhandler

override

public object invoke(object proxy, method method, object args) throws throwable

return method.invoke(target,args);Equivalent to someserviceimpldosome() method.

Use the jdk-like proxy to create ** objects to achieve the ability to create objects.

j**apackage com.mr.lee;

import com.mr.lee.dao.myinvocationhandler;

import com.mr.lee.service.someservice;

import com.mr.lee.service.someserviceimpl;

import org.junit.test;

import org.springframework.context.applicationcontext;

import org.springframework.context.support.classpathxmlapplicationcontext;

import j**a.lang.reflect.invocationhandler;

import j**a.lang.reflect.proxy;

public class apptest

testpublic void shouldanswerwithtrue()

Run the results. ``j**a

The current execution method is: dosome

Current execution time: 1664091059671

The dosome method was executed.

End time: 1664091059671

The current execution method is: doother

The doother method was executed.

process finished with exit code 0

4. CGLIB News**.

The underlying implementation of APO is dynamic, and AOP is a kind of normalization of dynamic.

The principle is to implement inheritance: create a subclass, the subclass is the ** object, and the target class and method must not be final,

Related Pages