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.

circle-check

Create business classes:

circle-check
circle-check

Create cross-cutting concern classes:

circle-check
circle-check
circle-check

circle-check
circle-check

Create AOP proxy-based class:

circle-check

Create application:

circle-check

Output:

circle-check

Last updated