Spring AOP Profiling Example

In this tutorial you will how to implement Profiling in Spring AOP.


Share on Google+Share on Google+

Spring AOP Profiling Example


In this tutorial you will how to implement Profiling in Spring AOP.

Spring AOP Profiling Example

In this example you will learn about implementation of Spring AOP profiling and can explain how it can be utilised. In this example The profiling aspect will record the start time and end time of each method invocation thus allowing more valuable statistics to be derived at a later point in time.


package roseindia.net;

public class SimpleBean {
	public void sayHi() {

	public void doOtherWork() {
		System.out.println("Doing Other Work");


package roseindia.net;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.util.StopWatch;

public class ProfileInterceptorClass implements MethodInterceptor {

	public Object invoke(MethodInvocation methodInvocation) throws Throwable {
		// TODO Auto-generated method stub
		StopWatch stopWatch = new StopWatch();
		Object object = methodInvocation.proceed();
		timePeriod(methodInvocation, stopWatch.getTotalTimeMillis());
		return object;

	private void timePeriod(MethodInvocation methodInvocation, long time) {
		String methodName = methodInvocation.getMethod().getName();
		System.out.println(methodName + "took time" + time);



package roseindia.net;

import org.aopalliance.aop.Advice;
import org.springframework.aop.framework.ProxyFactory;

public class MainClaz {
	public static void main(String[] args) {
		SimpleBean simpleBean = getSimpleBean();

	private static SimpleBean getSimpleBean() {
		SimpleBean simpleBean = new SimpleBean();
		ProxyFactory proxyFactory = new ProxyFactory();
		Advice advice = new ProfileInterceptorClass();
		SimpleBean proxy = (SimpleBean) proxyFactory.getProxy();
		return proxy;

When you run this application it will display message as shown below:

Doing Other Work

Download this example code


Posted on: September 13, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles