Building an AOP framework

In the example below we will write our own code to create AOP Framework using JDK Proxy without using any third-party libraries.

To use JDK Proxy we perform the following steps:

  • Create Invocation Handler: this class must implemnet java.lang.reflect.InvocationHandler. InvocationHandler is the interface implemented by the invocation handler of a proxy instance. When a method is called on a proxy instance, the method call is encrypted and sent to its invocation handler's calling method.

  • Create Proxy Instance : use Proxy.newProxyInstance() method provided by java.lang.reflect.Proxy factory method.

For example, we need to add some handling before and after the methods in the AccountService class are called.

Create Domain class.

Create business classes:

Create cross-cutting concern classes:

Create AOP proxy-based class:

Create application:

Output:

Last updated