This example shows you how to configure Spring AOP method profiling. We can use Spring AOP with any method in any service (or other) classes without writing a single line of profiling code in any of the service classes. Aspect Oriented Programming (AOP) allows us to separate the (usually duplicated and boilerplate) profiling code from the service code.

Spring AOP Method Profiling

We write our profiler code only once in a separated class (SimpleProfiler.java), and that’s all, the rest is only AOP configuration in spring.xml that has to be done for working.

So we are able to do method profiling for following.

  1. profiling any (service) classes,
  2. without touching (service) classes’ code,
  3. through Spring-AOP approach.

Spring AOP method profiling project structure

Tutorial-SpringAop-Profiling-projectStructure1

Spring AOP Maven dependencies

We will first look into our pom.xml file for all the required dependencies and their versions. We are using Spring 4 in this example.

  • We need spring-context as Spring dependency.
  • spring-aop also comes with spring-context as it’s dependent library, so no need to add it.
  • aspectjweaver is spring-aop’s dependency, but we have to add it since it is not defined explicitly for spring-aop.

Spring Service class (to be profiled)

We write our service class first, that has methods for simulate short and long processes, and one more that throws exception.

Please note that we didn’t put any profiling code in the service class.

Spring AOP Profiler class

Our profiler class do a simple measurement of the ms of processing time for a method execution.

  1. As you can see our profile method use a Stopwatch for measuring method execution.
  2. The method receives ProceedingJoinPoint object from Spring-AOP that represents a method execution joint point in this case before executing the method. This object has informations also about the method to be executed that we can get.
  3. We are responsible for executing the method by calling it’s proceed().
  4. We do aware about exceptions thrown also in the executed method for logging it, but we throw it forward to be transparent, we do not want to handle exceptions in this level, only logging it.

(The “profile” method signature is bounded as Spring AOP will call it, but the method name can be anything else that will be set in spring.xml.)

Spring AOP configuration XML

Now we have ready with every working classes needed in our mini application. Let’s create Spring’s configuration file:

spring.xml

  1. First we tell spring that we want to use classpath scanning for Spring components (rather than unconveniently defining them one by one in this xml), and also we enable Spring annotation detection.
  2. We configure Spring-AOP, to catch all methods in all classes in service package:
    1. We define an aspect that “pointcut”-s every service method,
    2. and we define an “around” method for these pointcuts that targets our SimpleProfiler’s “profile” named method.

Spring AOP Method Profiling Test Class

That’s it! The rest is only a testing class for our application.

You can see how simple we can start the Spring container from a main method, and getting our first dependency injected entry point, the service class instance.

If you run, you get below log:

You can see that profiling logs generated for every service method call. You can create other service classes in the same package or below, that will be also profiled.

If you want to learn how AOP concepts works check below reference link.
If you use log4j.properties file from attached source, and uncomment first line(s) you can see what’s going on under the hood.

References:
Aspect Oriented Programming with Spring

You can download the full maven project source from below link.

By admin

Leave a Reply

%d bloggers like this: