Spring AOP and Proxy Design Pattern

The Proxy Design Pattern is a structural design pattern that provides a surrogate or placeholder for another object to control access to it.

A Proxy maintains a reference (by implements the same interface) that lets the proxy access the real subject. The proxy controls access, creation, and deletion of the real subject.

image.png

By the proxy design pattern, we can control access to an object so it is to defer the full cost of its creation and initialization until we actually need to use it.

Spring AOP is a proxy-based AOP framework. This means that to implement aspects to the target objects, it'll create proxies of that object. Spring AOP uses JDK dynamic proxies and CGLIB to create the proxy for a target object. Creating the proxy by using JDK dynamic proxies is recommended referring to the Spring official document.

If the target object to be proxied implements at least one interface then a JDK dynamic proxy will be used. All of the interfaces implemented by the target type will be proxied. If the target object does not implement any interfaces then a CGLIB proxy will be created.


The proxy holds a generic type of target object that can stand for several types of its subclass objects. From the proxy, we can add more business logics between the target object method invocation to be organized as a Spring Aspect.

A Simulation of Spring AOP

Here's a simulation of the Spring AOP theory by using Java Reflect and Proxy Pattern: Assume we have a POJO class called Car: image.png

Data Access Object

Let's create a Data Access Object (DAO) and a DAO implementation of it:

image.png

image.png

The DAO implementation has a create method that can do some business logics, here we only output a line.

Proxy Handler

Then we can create a proxy handler called MyProxyHandler that implements the interface InvocationHandler and its invoke method:

image.png

The ProxyHandler holds targetObject which can be a reference to any Object Types. The now the beforeMethod and afterMethod combined together with the method invocation became a Spring Aspect.

Testing

Now we can create the main method to test it:

image.png

In the main method, we created a MyProxyHandler object as the proxy handler object, passing in the Dao object so the proxy handler now holds a reference to the CarDao. We create a proxy object under the CarDao type by using the Proxy of the Java Reflection to return the CarDao instance with the InvocationHandler of the class.

Please refer to the Java Doc to fill the three parameters:

Parameters

loader the class loader to define the proxy class

interfaces the list of interfaces for the proxy class to implement

h the invocation handler to dispatch method invocations to


Execution Result:

image.png

From the result we can see the aspect we made includes the beforeMethod, the testing method and the afterMethod. It provides the pluggable way to dynamically add the additional concern before, after or around the actual logic. With the SPring AOP we can define the additional concern like maintaining log, sending notification etc. in the method of a class.

Reference:

docs.spring.io/spring-framework/docs/3.0.0...

refactoring.guru/design-patterns/proxy

Design Patterns - elements of reusable object-oriented software, by ErichGamma, Richard Helm, Ralph Johnson and John Vlissides