JDK vs CGLib in Spring AOP: Which Proxy Is Faster?

This article explains the fundamentals of Spring AOP's JDK and CGLib dynamic proxies, compares their implementation details, presents performance test results across Java 1.6‑1.8, and offers practical guidance on choosing the appropriate proxy based on use‑case.

Java Backend Technology
Java Backend Technology
Java Backend Technology
JDK vs CGLib in Spring AOP: Which Proxy Is Faster?

Background

During a recent interview a candidate was asked which dynamic proxy—JDK or CGLib—offers higher efficiency in Spring AOP. This article summarizes the key points for reference.

Basic Concepts

Spring AOP can generate proxies using two mechanisms: JDK dynamic proxies (interface‑based) and CGLib (subclass‑based). JDK proxies rely on java.lang.reflect.Proxy and InvocationHandler, while CGLib uses bytecode generation to create a subclass that intercepts method calls.

JDK vs CGLib Differences

JDK proxy creates an implementation of the target’s interfaces; it cannot proxy classes without interfaces.

CGLib creates a subclass of the target class, allowing proxying of concrete classes (except final ones).

Performance Comparison – Textbook Description

Common wisdom states that CGLib proxies execute faster (up to 10×) but take longer to create (about 8× slower) compared to JDK proxies. Consequently, CGLib is preferred for singleton or pooled instances, while JDK proxies suit lightweight, frequently created objects.

Performance Test Setup

The following Java files were used for benchmarking:

Various Java classes (illustrated in images).

Target.java
TargetImpl.java
JdkDynamicProxyTest.java
CglibProxyTest.java
ProxyPerformanceTest.java

Images of the source code are shown below:

Test Results

Across Java 1.6, 1.7, and 1.8 the following observations were made:

In JDK 1.6 and 1.7, JDK proxies were slower than CGLib proxies, but the gap was far smaller than the textbook‑claimed tenfold difference.

In JDK 1.8, JDK proxies consistently outperformed CGLib proxies after multiple runs.

Thus, while JDK proxies have improved in newer JVMs, the choice still depends on the specific scenario.

Conclusion

The experiments show that JDK dynamic proxies are slower than CGLib in older Java versions, but in Java 1.8 they become faster. Developers should select the proxy type based on whether the target is an interface implementation (prefer JDK) or a concrete class (use CGLib), considering creation cost and runtime performance.

References: 1. https://blog.csdn.net/qq1723205668/article/details/56481476 2. https://blog.csdn.net/xiangbq/article/details/49794335

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaperformanceaopspringDynamic ProxycglibJDK Proxy
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.