JDK dynamic proxies and CGLIB proxies

The proxy in Spring AOP can be created in two ways:

  • JDK proxy (dynamic proxy): The JDK proxy creates a new proxy object by implementing interfaces of the target object and delegating method calls
  • CGLIB proxy: The CGLIB proxy creates a new proxy object by extending the target object and delegating method calls

Let's look at these proxy mechanisms and how they differ in the following table:

JDK proxy CGLIB proxy

It is built in JDK.

It is a custom-developed library.

JDK proxy works on the interface.

CGLIB proxy works on subclassing. This is used when the interface is not present.

It will proxy all interfaces.

It cannot work when the method and class are final.

From Spring 3.2, the ...

Get Hands-On High Performance with Spring 5 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.