I have already written about the Spring Aspect Oriented Programming (AOP) with the use of simple logging example. But, somehow I felt that the example code was little confusing for the newcomers. So, I decided to write a new Spring AOP example code that explains how to use Spring AOP for profiling method execution time.
Using Spring AOP to profile method execution time:
The example I chose is very simple: our application has a business class and a business method. We are going to profile how much time does it take to execute the business method. We will use Spring AOP for profiling the execution time. Since, profiling is a best example for cross cutting concern, it’s a good decision to use Spring AOP for implementing it. I’ll explain these in step by step. Meanwhile, you can download the source code(excluding dependent JAR file, for which you can see the download links below).
1. Setting up the project structure:
Launch your preferred IDE (I use Eclipse) and Create New Java Project. And then create the file structure as shown below (off course, no need to follow the package naming convention.
You need to download all the JARs that are in Referenced Libraries and add them to the project’s class path. You can find these JARs here: Spring, Commons-logging, aspectjrt, aspectjweaver. Also, please note that the file Business.java is an Interface. Other than this, all other java files are plain Classes.
2. Writing our Business Logic:
We will first write our business logic and then we will add Spring AOP to profile our business methods. Open Business.java interface and copy the below code into it.
Now, open the BusinessImpl.java and copy the below code into it.
I guess this code is self explanatory. Our business method just sleeps for 2 seconds (good business, isn’t it!?) and then write some text on the console.
3. Writing a Spring Aspect to profile business method:
Lets write a Aspect which will profile our business method. I’m gonna use @Around advice (Lost in the jargons? Spring AOP basics). Open the BusinessProfiler.java file and copy the below code into it.
Here’s what this code does:
- Using @AspectJ annotation, we have declared that this class is an Aspect.
- Using @Pointcut annotation, we have defined a pointcut that will match the execution of all public method inside com.veerasundar.spring.aop package. [read more about pointcut]
- Using @Around annotation, we have defined a Around advice which will be invoked before and after our business method. i.e. @Around advice will wrap our business method. As you can see in the above code, pjp.proceed(). This is when our business method gets called from @Around advice.
4. Configuring Spring AOP and @AspectJ support:
Open the applicationContext.xml file and copy the below code into it.
Here’s what we are doing in the above XML file:
- We have added the required AOP schemas on the top of the XML file.
- Using , we’ve enabled the @AspectJ support to our application.
- And then we defined two normal Spring beans - one for our Business class and the other for Business Profiler (i.e. our aspect).
That’s it. With all the above code is done, we have successfully added Spring AOP support to our project and configured it. In the next step, lets test our code.
5. Testing the Spring AOP profiler:
Open the file SpringAOPDemo.java and copy the below lines into it.
In the above code we are loading our Business bean from Spring Context and then calling our business method. If you run this class, it will produce the following output on the console.
I hope this tutorial helps you to understand the basics of Spring Aspect Oriented programming. If you have any questions regarding the above example, feel free to leave a comment.