DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowHalloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
FREEDOM251

Freedom251 › Blog › CGLIB

Spring JDK Dynamic Proxies vs CGLIB: Choose the Right Proxy and Fix AOP Traps

By Freedom251 Team · · 11 min read

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.

Spring normally chooses a JDK dynamic proxy when a bean implements at least one interface, and a CGLIB subclass proxy when it implements none. You can force class-based CGLIB proxying with proxy-target-class=true. Choose JDK proxying when an interface is the complete contract your callers need; choose CGLIB when advice must reach concrete-class methods or callers must inject the implementation type. The choice is rarely a performance decision. More important are API visibility, subclassing constraints, configuration, and whether calls actually pass through the proxy.

What are Spring proxies, and why do they matter?

Spring AOP is proxy-based. Instead of rewriting the target method, Spring places a proxy in front of the bean. Calls that enter through that proxy can trigger advice such as transactions, caching, security checks, logging, or asynchronous execution before and after the target method runs.

As an Amazon Associate I earn from qualifying purchases.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The object you receive from the application context may therefore not be the concrete object you wrote. It may be an interface proxy that delegates to the target, or a runtime-generated subclass of the target. That difference affects which methods callers can see, what type can be injected, which methods can be advised, and whether a call reaches the advice chain at all.

The practical rule is simple: design around the API your callers need, then verify that the selected proxy can expose and override every method that must be advised. Do not choose a proxy style merely because its generated class name looks familiar.

#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter | 4Pack Type C Female to A Male Cable Converter for Apple Duo 17 16 15 14 Plus,iWatch Watch 12 11 10,iPad 9 8 Air,Airpods 5 4
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.

How does Spring decide between a JDK proxy and CGLIB?

With Spring’s normal automatic choice, an interface leads to a JDK dynamic proxy and the absence of an interface leads to a CGLIB proxy.

When the target implements an interface

Spring uses a JDK dynamic proxy when the target implements at least one interface. The proxy presents the interface contract to callers and delegates those operations to the target object. Methods that exist only on the concrete implementation are not part of that proxy’s interface surface.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For example, if InvoiceServiceImpl implements InvoiceService but also declares rebuildSearchIndex() only on the implementation, a caller holding the JDK proxy can use the methods declared by InvoiceService, not the implementation-only method.

When the target implements no interface

Without a target interface, Spring creates a CGLIB proxy. CGLIB generates a subclass at runtime, allowing the proxy to expose concrete methods that can legally be overridden.

This is useful for class-based services, but it does not remove Java’s inheritance rules. The target class must be subclassable, and each method that needs advice must be visible and overridable.

When configuration forces class-based proxying

Setting proxy-target-class=true tells Spring to use class-based CGLIB proxying even when the target also implements interfaces. In Java configuration, the equivalent is:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
@EnableAspectJAutoProxy(proxyTargetClass = true)
@Configuration
class AopConfig {
}

XML configuration can express the same intent with the proxy-target-class="true" attribute on the relevant auto-proxy element. Transaction auto-proxy configuration can also participate in this choice.

Spring combines several auto-proxy facilities through a unified auto-proxy creator. If multiple configuration elements contribute settings, the strongest setting applies. A single component enabling class-based proxying can therefore affect other advice facilities that use that creator, not just the aspect you were editing.

What is the difference between JDK dynamic proxies and CGLIB?

Both proxy styles can run the same Spring advice. Their important difference is how they represent the target and what Java members they can expose or override.

Rank #2
Sale
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter | USB C Dongle with 90W Max Power Delivery, 3×5Gbps USB A Data Ports, for MacBook, Dell, and More (Charger Not Included)
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.
Axis JDK dynamic proxy CGLIB proxy
Generation model Runtime-generated object implementing interfaces Runtime-generated subclass of the target class
Spring’s automatic trigger Target implements at least one interface Target implements no interface
Best fit The interface fully describes the operations callers need Advice must cover concrete methods, or callers require a concrete type
Visible API Interface methods exposed by the proxy Overridable methods inherited from the target class; interfaces may also be implemented
Type requirement Callers should depend on an exposed interface Target must be subclassable and the injection type must be compatible with the generated subclass
Methods that cannot be advised Methods not on an exposed interface Final, private, or otherwise non-visible methods, plus methods that cannot be overridden
How to select explicitly Default interface-oriented behavior proxy-target-class=true, or automatic selection when no interface exists
Performance No documented decisive advantage No documented decisive advantage

The table describes proxy capability, not a guarantee that every eligible method is advised. The call still has to enter through the proxy, and the method must match the pointcut and other advice conditions in your application.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When should you choose a JDK dynamic proxy?

Choose the JDK style when the interface is a complete and intentional boundary for the service.

The interface is the contract

Interface-oriented proxying keeps callers coupled to behavior rather than implementation details. A controller, scheduled job, or other service can depend on PaymentService without knowing which class implements it. The proxy naturally preserves that boundary.

You want implementation-only methods hidden

If a concrete class contains helper or maintenance operations that should not be part of the service API, a JDK proxy prevents callers holding the proxied reference from treating those methods as public service operations.

You want fewer inheritance constraints

JDK proxies do not need to subclass the target. A class that cannot be subclassed is not automatically disqualified from interface-based proxying, provided the required operations are on an interface and the proxy is used through that interface.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Do not use a JDK proxy when an advised operation exists only on the implementation class or when an injection point explicitly requires that concrete class. In those cases, either move the operation into the interface deliberately or use class-based proxying.

When should you choose CGLIB?

Use CGLIB when the concrete class is the required boundary and its methods are suitable for subclass interception.

The target has no interface

For a class with no interface, CGLIB is Spring’s normal automatic option. This lets Spring apply method advice without inventing a separate interface solely for proxying.

An advised method exists only on the class

If callers legitimately need a concrete-class method and that method must be advised, a JDK proxy cannot expose it. A CGLIB subclass can proxy it when the method is visible and overridable.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter | 85W Max PD, 2 USB 3.0 & 1 USBC Data Ports, SD/TF Card Reader, for Laptops/Mac and Other Type C Devices (Charger Not Included)
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.

A concrete injection type is required

Some injection boundaries are written against the implementation type rather than an interface. A JDK proxy is not a substitute for that concrete type, while a compatible CGLIB subclass can satisfy the class-based boundary when the target is subclassable.

That convenience comes with stronger constraints. Before forcing CGLIB, check the class declaration and every method that must receive advice. A final class cannot be subclassed, and final or private methods cannot be overridden for interception. Non-visible methods are excluded as well.

What does proxy-target-class change?

proxy-target-class changes the proxying model from interface-oriented selection to class-based CGLIB proxying. It is not a switch that makes self-invocation work, nor does it make final methods interceptable.

Java configuration

Set proxyTargetClass = true on @EnableAspectJAutoProxy when your configuration owns the aspect auto-proxy creator:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
@EnableAspectJAutoProxy(proxyTargetClass = true)
@Configuration
class AopConfig {
}

The spelling is different in Java and XML: Java uses proxyTargetClass, while XML uses the hyphenated proxy-target-class.

XML configuration

An XML auto-proxy element can opt into class-based proxies with:

<aop:aspectj-autoproxy proxy-target-class="true" />

Use the equivalent attribute on the transaction auto-proxy configuration when that facility is the one creating the proxy. Because Spring may consolidate these facilities into one auto-proxy creator, inspect all relevant configuration rather than changing only the annotation or XML element nearest the failing method.

Why a global-looking change can have broad effects

The unified creator means the strongest proxying setting can apply to several kinds of advice. Enabling class-based proxying for one concern may change the proxy type observed by beans receiving transactions, caching, security, or other proxy-based advice. Treat the setting as an application configuration decision, not merely a local annotation tweak.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Which methods can CGLIB actually advise?

CGLIB can intercept methods only where Java inheritance permits the generated subclass to override them.

Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray | Data transfer & Fast charging for MacBook, iPhone 17/16/15, iPad, Samsung Galaxy, Apple Watch
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
  • Final class: cannot be subclassed, so a CGLIB proxy cannot be created for it.
  • Final method: cannot be overridden, so subclass-based advice cannot intercept it.
  • Private method: is not inherited as an overridable method and cannot be intercepted through the subclass.
  • Non-visible method: methods that are not visible to the generated subclass are excluded.
  • Eligible method with no matching advice: may be proxyable but still run without advice if the pointcut does not match.

Changing a method from private to protected or public can make it structurally eligible, but that is an API and encapsulation decision. Do not widen visibility solely to hide an architectural problem; consider moving the operation behind a separate service boundary when it represents a distinct advised action.

Why does self-invocation skip @Transactional, @Async, caching, or security advice?

A call made through the proxy can run advice. A call from one method to another through this stays inside the target object and bypasses the proxy, so the advice chain is not entered.

The failing call path

Suppose processOrder() is advised and calls sendReceipt() as this.sendReceipt(). The external caller enters the proxy for processOrder(), but the internal call goes directly from the target object to its own method. Spring never gets a second proxy interception point for sendReceipt().

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Preferred fix: refactor the boundary

Move the operation that needs independent advice into another Spring bean and inject that bean. The call then crosses a bean boundary and enters the second bean through its proxy. This design also makes the transaction, asynchronous task, cache operation, or security check explicit in the service structure.

Alternative: self-injection

Inject the proxied bean reference and call the advised method through that reference instead of this. This can work, but it introduces a self-reference and should be documented so future refactoring does not replace the proxy call with a direct one.

Last resort: AopContext.currentProxy()

AopContext.currentProxy() requires the proxy to be exposed. It couples the class directly to Spring AOP and is discouraged by Spring’s guidance. Use it only when refactoring or self-injection is impractical and the coupling is an accepted trade-off.

Switching from JDK to CGLIB does not remove the self-invocation rule. Both are proxies, and neither can intercept a call that never passes through the proxy object.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How do proxy types affect injection and casting?

JDK proxies should be consumed through one of their exposed interfaces. If a dependency is declared as the concrete implementation, the interface proxy cannot satisfy that class-based expectation.

CGLIB proxies are subclasses, so a compatible concrete-class injection point can work, but only when the target class can be subclassed. Forcing CGLIB solely to satisfy a concrete injection point may solve the immediate type mismatch while increasing coupling to implementation details.

Best Value
Sale
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display | 85W Max PD, 1 Powered USB-C 5Gbps & 2 Powered USB-A 3.0 5Gbps Data Ports, for MacBook Pro, MacBook Air, Dell and More (Black)
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.

When an implementation-only operation is genuinely part of the service contract, expose it through a deliberate interface or use class-based proxying with a subclassable class. Avoid casting a proxied reference opportunistically; a cast that succeeds with one configuration can fail when another auto-proxy setting changes the proxy style.

Is JDK or CGLIB faster?

Spring’s reference guidance does not identify a decisive performance advantage for either style. The documented difference is described qualitatively as small, with no universal benchmark number that applies across JDK versions, JVMs, Spring releases, and workloads.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose based on API shape, method eligibility, and type requirements. If proxy overhead is material in a particular application, benchmark the actual JDK, JVM, Spring version, advice chain, call frequency, and target workload. A synthetic result from a different environment cannot establish a general winner.

How can you diagnose the wrong proxy or missing advice?

Follow the call path and configuration in a fixed order instead of changing proxy settings at random.

  1. Inspect the target API. Record every interface the bean implements and identify whether the failing method is declared on one of them.
  2. Check the effective proxy type. Determine whether the injected object is interface-based or class-based using your debugger or runtime type inspection. A JDK proxy indicates interface-oriented selection; a generated subclass indicates CGLIB.
  3. Search configuration. Look for proxyTargetClass = true, proxy-target-class="true", and transaction auto-proxy settings. Remember that the unified auto-proxy creator applies the strongest setting.
  4. Verify method eligibility. For CGLIB, remove final or private declarations only when that matches the intended design, and ensure the method is visible to the subclass. For JDK proxying, confirm the operation is on an exposed interface.
  5. Trace the caller. Confirm that the call comes through the Spring-managed proxy rather than a direct reference to the target or a this call from another method in the same class.
  6. Check the advice match. A proxy can be present while a pointcut, annotation condition, or method signature prevents the specific advice from matching.
  7. Retest the boundary. Call the method from an external bean through the injected reference. If advice appears externally but not internally, the remaining issue is self-invocation rather than proxy selection.

What is the safest design rule for Spring proxying?

Start with the caller-facing contract, not the proxy technology. If an interface completely describes the operations that need advice, let Spring use a JDK proxy and keep dependencies interface-based. If the application intentionally relies on concrete methods or class-based injection, use CGLIB only after confirming that the class and advised methods are subclassable.

Keep advised operations at bean boundaries so calls enter through a proxy. Treat proxy-target-class as a coordinated configuration choice because it can affect multiple auto-proxy facilities. Finally, test behavior through the same external call paths production code uses; inspecting only the target class can conceal proxy and self-invocation failures.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Which proxy should you use in common situations?

Situation Recommended approach Reason
Service has a complete interface and callers use it JDK dynamic proxy Preserves the explicit interface contract and avoids implementation coupling
Service has no interface Default CGLIB proxy Spring needs a class-based proxy to apply advice
Advice must cover a concrete method absent from interfaces CGLIB, if the method is overridable JDK proxies cannot expose implementation-only methods
Concrete-class injection is intentional CGLIB, if the class is subclassable The generated subclass can satisfy the class-based boundary
Target class or required method is final/private/non-visible Refactor the boundary or method; do not expect CGLIB to advise it Subclass interception cannot override those members
Only concern is presumed speed Keep the API-driven default and benchmark if necessary Spring documents no decisive general performance winner

Frequently Asked Questions

Can one application use JDK proxies for some beans and CGLIB for others?

The effective style depends on which auto-proxy creator handles each bean and on the settings contributed to that creator. Because Spring can consolidate several facilities into a unified creator, a class-based setting may have a wider effect than the single bean or advice you intended; verify the resulting proxy types instead of assuming complete isolation.

Will changing a proxy from JDK to CGLIB change transaction propagation rules?

Proxy style does not define propagation semantics. Once a call is intercepted, the transaction advice applies according to its own configuration; changing the proxy type mainly changes type exposure and which methods can be intercepted.

Should tests assert the generated proxy class name?

Usually no. Generated names and implementation details can vary with configuration, while the stable contract is whether an external call receives the expected advice and result. Test through the interface or intended class boundary and include a case that exercises an internal call when self-invocation is a risk.

Is forcing CGLIB a fix for advice that works only from some callers?

Not by itself. If the missing advice is caused by a call through this, both JDK and CGLIB proxies are bypassed. First prove that the invocation enters the proxy; then address the bean boundary or self-reference before changing proxy style.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

CGLIBJDK dynamic proxyproxy-target-classSpring AOPSpring Frameworktransaction management

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.