Proxy Design Pattern Using Spring AOP

 Proxy design pattern provides an object of a class with the functionality of another class with having it. This pattern comes under the structural design pattern of GOF Design Patterns.


Spring provides two ways to create the proxy in the application.

   1.JDK

   2.CGLIB                                                                                                                                                                       


                  JDK proxy                                                  CGLIB proxy

1) Also called dynamic proxies                       1) NOT built into JDK

2) API is built into the JDK                            2)  Included in Spring JARs

3) Requirements: Java interface(s)               3)   Used when interface not available

4)  All interfaces proxied                               4)  Cannot be applied to final classes or methods 


Spring's Aspect-Oriented Programming (AOP) with annotations to create a proxy, you can utilize the @Aspect annotation along with pointcut expressions to specify where the advice (additional behavior) should be applied. Here's an example:


UseCase: Log the execution time of any method which is declared with LogExecutionTime custom annotation.

To demonstrate a more specific use case with an annotation, let's create an @LogExecutionTime annotation that can be used to annotate methods you want to log the execution time for.


Let's break down the example step by step:

1.Define the Annotation:


In this step, we create a custom annotation @LogExecutionTime that can be applied to methods.

2.Create the Service Interface and Implementation:





In this step, we define a simple service interface MyService with two methods. One method (doSomething) represents a regular method, and the other metho (annotatedMethod) is annotated with @LogExecutionTime.

3.Create the LoggingAspect:





In this step, we create the LoggingAspect class. It is annotated with @Aspect and @Component. We define a pointcut (logExecutionTimeAnnotation) that matches methods annotated with @LogExecutionTime. The @Around advice logs method entry, exit, and execution time.

4.Configure Spring Boot Application:



In this step, we configure the Spring Boot application. The
@EnableAspectJAutoProxy annotation enables AspectJ proxy support.


5.Create a Controller to Use the Service:




  1. In this step, we create a simple controller (MyController) that uses the MyService interface.

Now, when you run your Spring Boot application and make requests to /doSomething and /annotatedMethod, you'll observe log entries in the console generated by the LoggingAspect. The aspect intercepts the method calls and provides additional behavior based on the presence of the @LogExecutionTime annotation, demonstrating the proxy design pattern in action.


Similarly we can add more aspects based our requirements apply the logic on the fly to a specific method.
Example: We can log parameters using our own annotation like @LogParameter with respective aspect. etc...

Comments

Popular posts from this blog

Strategy Design Pattern

Use cases of Supplier in java 8