Spring Aspect Oriented Programming - Adding a simple profiler to your services layer
There is a lot of traction on aspect oriented programming and I have used spring for declaritive transcations, but I hadn’t used any other sort of aspects. Someone asked for a Profiling mechanism and so I looked into gettign this set up. There is a lot of documentation but with so many ways to do something it can be a bit confusing. I chose to do this via XML not annotations. I felt the xml makes things more configurable since I want to run the profiling advice only in development and not in production.
Some assumptions your using Spring 2.5, maven and proabably eclipse 3.3+. If any of this is not the case of course these directions will differ slightly but probably not that much.
First of all I decied to go with the AspectJ annotations as oopposed to the Spring AOP, mainly because in Spring 2.5 this seems to be better documented. So to do this you need some additional aspectj dependencies. So if your using maven you add these in your pom.xml. Otherwise you need to pull down these jars from mvnrepository.com.
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.5.4</version>
</dependency>
Then do a mvn –U eclipse:eclipse of course
Then I added a SimpleProfiler class (copied straight from Springs site) to my eclipse project.
package com.cms.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.util.StopWatch;
public class SimpleProfiler {
public Object profile(ProceedingJoinPoint call)
throws Throwable {
StopWatch clock = new StopWatch(call.getTarget()+"."+call.toShortString());
try {
clock.start(call.toShortString());
return call.proceed();
} finally {
clock.stop();
System.out.println(clock.prettyPrint());
}
}
}
Then added to your applicationContext.xml or beans.xml
<aop:aspectj-autoproxy/>
<bean id="profiler" class="com.cms.aop.SimpleProfiler"/>
<aop:config>
<aop:aspect ref="profiler">
<aop:pointcut id="managerMethod"
expression="execution(* com.cms.*.*Manager.*(..))" />
<aop:around pointcut-ref="managerMethod"
method="profile"/>
</aop:aspect>
</aop:config>
All of our service layer is in classes that end with Manager so that’s explains the regex expression.
And voila you have some profilnig information for your service layer, for all the classes defined as Spring Beans in the package com.cms.* that end in Manager.
StopWatch 'com.cms.gl.GeneralLedgerManagerImpl@6691da.execution(getGlmasterSearchResults)': running time (millis) = 79
-----------------------------------------
ms % Task name
-----------------------------------------
00079 100% execution(getGlmasterSearchResults)
Heres the spring documentation
http://static.springframework.org/spring/docs/2.5.x/reference/aop.html
Re: Spring Aspect Oriented Programming - Adding a simple profiler to your services layer
jetm.void.fm/howto/spring_2_x_integration.html
It also offers a nice console for viewing the results.
Bruce
