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. |