Dynamic proxy-based AOP
Spring AOP, when first presented to Java programmers, seems like magic. How does it happen that we have a variable of class
X
and we call some method on that object, but instead, it executes some aspect before or after the method execution, or even around it, intercepting the call
The technique that Spring does is called dynamic proxy. When we have an object, which implements an interface, we can create another object—the proxy object—that also implements that interface, but each and every method implementation invokes a different object called handler, implementing the JDK interface, InvocationHandler
. When a method of the interface is invoked on the proxy object, it will call the following method on the handler object:
public Object invoke(Object target, Method m, Object[] args)
This method is free to do anything, even calling the original method on the target object with the original or modified argument.
When we do not have an interface at hand that the class to be...