Ondrej Bozek
Ondrej Bozek

Reputation: 11481

Advantages of JDK dynamic proxies

What are the advantages of JDK dynamic proxies? Especially in comparison with CGLIB proxies. Despite the fact that JDK proxies don't bring dependency on any third party library, what are the other advantages of JDK proxies?
I read on few places that CGLIB proxies are slightly faster than JDK proxies. Also CGLIB proxies doesn't require proxied class to implement any interface. From this it seems that CGLIB proxies are superior to JDK proxies. Is that true?

Upvotes: 4

Views: 1096

Answers (2)

Stephen C
Stephen C

Reputation: 718856

I just want to know use cases for JDK dynamic proxies and their advantages.

The use-cases are roughly the same/

Or are they simply deprecated?

No. JDK dynamic proxies are not deprecated. Check the javadoc.

Should I always use CGLIB instead?

No ... not always. You have already identified one case where you wouldn't. And @Evgeniy Dorofeev. Has identified another ... which was also identified in the Question I linked to!

A third thing to consider is which version of CGLIB to use, and whether it is compatible with other things you are using. (One would expect JDK dynamic proxies to be more stable than CGLIB ... based on where it comes from. Performance is not the only criterion for choosing.)

But basically, it is up to you to make your own decision ... based on your actual system requirements, and not some random recommendations.

Upvotes: 1

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136022

One disadvantage of proxying classes with CGLIB is that it cant work with final methods as they cannot be overridden.

Upvotes: 0

Related Questions