Home Common Problem What is the difference between cglib proxy and jdk dynamic proxy?

What is the difference between cglib proxy and jdk dynamic proxy?

Jun 12, 2023 am 10:36 AM
jdk cglib

The difference between cglib proxy and jdk dynamic proxy is that JDK dynamic proxy creates proxy instances of the proxy object based on interfaces, while Cglib proxy generates subclasses of the proxy class based on inheritance.

What is the difference between cglib proxy and jdk dynamic proxy?

Operating system for this tutorial: Windows 10 system, jdk15 version, Dell G3 computer.

JDK dynamic proxy and Cglib proxy are both implementation methods of proxy in Java language. There are certain differences between the two in terms of usage, performance and applicable scenarios:

1. JDK dynamic Proxy:

屏幕截图 2023-06-12 103510.png

  • Create a proxy instance of the proxied object based on the interface. When an object is to be proxied, it must implement one or more interfaces and rely on the JDK library. JDK dynamic proxy uses the reflection mechanism to generate a proxy class that contains all interfaces of the proxy object, and covers all methods in the interface, which can proxy the target object.

  • Advantages: No need to reference third-party libraries, it can be run in the JRE runtime environment, and generating proxy objects is simpler and faster; Disadvantages: Only proxying based on interfaces is supported, and classes cannot be agent, so its role is limited.

2. Cglib proxy:

屏幕截图 2023-06-12 103555.png

  • Generate subclasses of the proxy class based on inheritance , thereby adding proxy logic. Because it inherits the proxied class, it will be affected by non-inheritable attributes such as final classes, private, and static.

  • Advantages: Cglib supports proxying classes. Even if there is no interface, it can be achieved indirectly by setting a callback interface. The performance is higher than JDK dynamic proxy, and it can proxy target objects that do not implement any interface.

In the process of generating proxy classes, Cglib adopts the method of dynamically generating bytecode. The proxy class is created and cached in memory before being loaded by the proxy class. On each call, the cached proxy class is used directly. In most cases, Cglib proxies are more suitable for large-scale method interception and enhancement scenarios than JDK dynamic proxies.

The above is the detailed content of What is the difference between cglib proxy and jdk dynamic proxy?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to install Java on Windows 11 How to install Java on Windows 11 Apr 13, 2023 pm 09:22 PM

Despite the large number of applications that appear every once in a while, Java remains by far one of the most used and important programming languages. Many applications rely on Java on Windows operating systems, and updating it means improving performance by providing stability and security for Java applications to run safely. You can also install Java on Linux and macOS platforms. The only difference is the package/file for each platform. Now, with Windows 11, it’s time to download Java and in today’s article, we will take you through the simple steps to install it on your device. Which version of Java should I download? The Java version you downloaded

Does oracle database require jdk? Does oracle database require jdk? Jun 05, 2023 pm 05:06 PM

The oracle database requires jdk. The reasons are: 1. When using specific software or functions, other software or libraries included in the JDK are required; 2. Java JDK needs to be installed to run Java programs in the Oracle database; 3. JDK provides Develop and compile Java application functions; 4. Meet Oracle's requirements for Java functions to help implement and implement specific functions.

Analysis of JDK dynamic proxy examples in java Analysis of JDK dynamic proxy examples in java Apr 30, 2023 pm 01:16 PM

1. Explain that Java provides a dynamic proxy class Proxy. Proxy is not the class of what we call proxy objects, but provides a static method (newProxyInstance) to create proxy objects to obtain proxy objects. 2. Instance publicclassHelloWorld{publicstaticvoidmain(String[]args){//Get the proxy object ProxyFactoryfactory=newProxyFactory();SellTicketsproxyObject=factory.getProxyObject();proxyO

Deepin Linux system installation JDK tutorial Deepin Linux system installation JDK tutorial Feb 15, 2024 pm 12:36 PM

Deepin Linux system is a domestic operating system based on the Linux kernel. It has the characteristics of stability, security, and ease of use. In Deepin Linux system, installing JDK (Java Development Kit) is a necessary step for developing Java applications. This article will introduce in detail how to Install JDK in Deepin Linux system. Installation steps: Open the terminal of Deepin Linux system. Use the command line to download the JDK installation package. The command is as follows: ``shellsudoapt-getinstallopenjdk-11-jdk`` Wait for the download to complete and the system will automatically install the JDK. To verify whether the JDK is installed successfully, enter the following command: ```javaj

Where is the linux jdk directory? Where is the linux jdk directory? Mar 22, 2023 am 09:52 AM

The linux jdk directory is in the bin directory. The specific search method is: 1. Find the execution directory of javad through the "whereis java" command; 2. Find the link file through the execution file; 3. Through "ls -lrt /etc/alternatives/java" Just run the command to find the installation directory.

How to install jdk-jdk installation tutorial How to install jdk-jdk installation tutorial Mar 04, 2024 pm 05:10 PM

Recently, many friends have asked me how to install jdk. Next, let us learn all about how to install jdk. I hope it can help everyone. 1. First download the JDK installation file and enter the JDK installation interface, as shown in the figure. 2. Click the "Next" button to enter the JDK custom installation interface, as shown in the figure. 3. It is recommended to choose to install directly to the default directory and click the "Next" button to install, as shown in the figure. You can also click the "Change" button to select the installation directory yourself. 4. After the installation is completed, the interface will pop up and click the "Close" button, as shown in the figure. The above is all the tutorials on how to install jdk brought by the editor. I hope it can be helpful to everyone.

How to configure the jdk environment in Linux system How to configure the jdk environment in Linux system May 12, 2023 am 09:31 AM

The following steps are to deploy the jdk environment in the Linux system 1. Download the jdk installation package 2. Create a new installation jdk folder (/usr/local/java/jdk) cd /usr/localmkdir/usr/local/javalocal directory and create a new java directory cd /usr/local/javamkdir/usr/local/java/jdk Create a new jdk directory cd/usr/local/java/jdk3. Upload the jdk installation package to linux4. Unzip the jdk installation package. Decompression format: The name after the tarzxvf compressed package name can be just Enter an initial letter and use the Tab key

How to use Cglib proxy in java How to use Cglib proxy in java Apr 30, 2023 pm 06:04 PM

Note 1. The Cglib agent can be called a subclass agent, which builds subclass objects in memory to extend the functionality of the target object. Both static proxies and JDK proxies require an object to implement an interface. Sometimes the proxy object is just a single object, and Cglib proxy can be used at this time. 2. Cglib generates a proxy class through Enhancer and implements the intercept method by implementing the MethodInterceptor interface. Enhancement methods can be added to the method and the original method can be called using a reflective Method or MethodProxy inherited class. Example publicclassTVProxyCglibimplementsM