What are Dynamic Proxy classes and why would I use one?

cwash picture cwash · Jun 1, 2009 · Viewed 55.1k times · Source

What is a use case for using a dynamic proxy?

How do they relate to bytecode generation and reflection?

Any recommended reading?

Answer

xiaojieaa picture xiaojieaa · Jan 26, 2014

I highly recommend this resource.

First of all, you must understand what the proxy pattern use case. Remember that the main intent of a proxy is to control access to the target object, rather than to enhance the functionality of the target object. The access control includes synchronization, authentication, remote access (RPC), lazy instantiation (Hibernate, Mybatis), AOP (transaction).

In contrast with static proxy, the dynamic proxy generates bytecode which requires Java reflection at runtime. With the dynamic approach you don't need to create the proxy class, which can lead to more convenience.