Before we jump into the discussion of Java dynamic proxies it may be worth restating the purpose of the proxy pattern. The intent of a proxy is to provide a surrogate or placeholder for another object to control access to it.
In other words proxies are usually objects that reside between an interface and an implementation and intercept interface calls before they pass them to implementations.
The traditional way of implementing a proxy is to create a class that implements the desired interface(s) and forwards calls to the actual implementation. This means one proxy class per every interface/implementation pair which leads to a proliferation of classes. It also means that each proxied method has to be coded separately. For example:
public interface Foo
public void doSomething();
public void doAnother();
public class FooImpl implements Foo
public void doSomething() throws MyException
public void doAnother()
System.out.println("Doing something else...");
public class LoggingProxy implements Foo
private FooImpl target = null;
public FooProxy(FooImpl target)
this.target = target;
public void doSomething()
public void doAnother()
Now the above is hardly good news as we are forced to implement quite a lot of pretty repetitive code just to get some log statements out of our proxy. Rest assured however, we can do a lot better than this.
A much nicer version of the proxy can be implemented by making use of the
java.lang.reflect package. The most interesting class for our purposes is the unsurprisingly named
java.lang.reflect.Proxy paired with the interface
The invocation handler interface is simple enough to be quoted here in full:
public interface InvocationHandler
public Object invoke(Object proxy, Method method, Object args) throws Throwable;
That's it folks! That's all we'll need to implement for our proxy to work polymorphically on any method with any Interface/implementation pair. But in order to make use of the InvocationHandler one must also learn a bit about
Proxy class can be instantiated through a single static method
newProxyInstance which needs an instance of a
ClassLoader under which the proxy is to be defined, an array of interfaces (passed as an array of
java.lang.Class) and an instance of the InvocationHandler interface. We can combine this in a single class which we will call
public class DynamicProxy implements InvocationHandler
private Object target = null;
public static Object newInstance(Object target)
Class targetClass = target.getClass();
Class interfaces = target.getInterfaces();
return Proxy.newProxyInstance(targetClass.getClassLoader(), interfaces, new DynamicProxy(target));
private DynamicProxy(Object target)
this.target = target;
public Object invoke(Object proxy, Method method, Object args) throws Throwable
Object invocationResult = null;
System.out.println("Before method " + method.getName();
invocationResult = method.invoke(this.target, args);
System.out.println("After method " + method.getName();
//this is the exception thrown by the method being invoked
//we just rethrow the wrapped exception to conform to the
System.err.println("Invocation of " + method.getName() + " failed");
A few words of explanation about the invoke method are in order here, especially around exception handling. The catch block intercepts two types of exceptions. The
InvocationTargetException is a java language exception that wraps around any exception that might get thrown in the target's method (e.g.
InsufficientFundsException thrown from
transferMoney()). All we need to do there is rethrow the wrapped exception in order to honour the original interface's contract. The second catch block catches anything that might have gone wrong with the invocation process itself. If target is a remote object there may be an Remote exception thrown for example. Dealing with those situations is usually application specific. The proxy if free to deal with the situation in any number of ways. For example, it might catch the exception and report an error for an administrator to inspect, it might rethrow the exception as a RuntimeException in which case the caller will receive the Exception in its first catch block, or in some more sophisticated implementations the proxy itself may try to recover from the error in some fashion (for example, by trying to reestablish the connection with the implementor).
DynamicProxy is trivial. It merely requires us to invoke the static
newInstance method whenever we want to instantiate an implementation. Using classes from our first example:
Foo aFoo = (Foo) DynamicProxy.newInstance(new FooImpl());
will result in
doAnother() are called. The console output should look as follows:
Before method doSomething
After method doSomething
Before method doAnother
After method odAnother
We accomplished the same as in the first, naive implementation but now we have a completely generic framework to wrap any method call in a system. The applications of this are endless and include but aren't limited to: logging, timing, debugging, security checking, transaction control etc.
The proxy pattern is the inspiration of the concept of Aspect Oriented Programming and dynamic proxies similar to the one shown are the core tool used in implementing AOP in java through projects like AspectJ.
Even if you are not implementing an EJB application server or working towards a Java implementation of AOP chances are a dynamic proxy can make your programmer's life a lot easier and help you cut down on a significant amount of boilerplate code which is inevitably a Good Thing.