Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] overridden method not caught

I am experimenting with the Spring integration for Aspectj as documented
at
http://static.springsource.org/spring/docs/2.5.x/reference/aop.html#aop-
using-aspectj and have come across a problem.

If I alter the class invocation to override the method that I want to
instrument, the method is no longer detected.

So, my class is:

package foo;

public class EntitlementCalculationService {
	public void calculateEntitlement() {
		System.out.println("executing");
		try {
			Thread.sleep(200);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

My aspect is:

package foo;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.util.StopWatch;

@Aspect
public class ProfilingAspect {
    @Around("methodsToBeProfiled()")
    public void profile(ProceedingJoinPoint pjp) throws Throwable {
        StopWatch sw = new StopWatch(getClass().getSimpleName());
        try {
            sw.start(pjp.getSignature().getName());
            pjp.proceed();
        } finally {
            sw.stop();
            System.out.println(sw.prettyPrint());
        }
    }

    @Pointcut("execution(public * foo..*.*(..))")
    public void methodsToBeProfiled(){}
}

And my code to test is:

package foo;

import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;

public final class Main {
	public Main() {
         	ApplicationContext ctx = new
ClassPathXmlApplicationContext("/beans.xml", Main.class);

		EntitlementCalculationService
entitlementCalculationService = new EntitlementCalculationService();{

       	entitlementCalculationService.calculateEntitlement();
	}

	public static void main(String[] args) {
		new Main();
    	}
}

This gives the correct output:

     [java] StopWatch 'ProfilingAspect': running time (millis) = 201
     [java] -----------------------------------------
     [java] ms     %     Task name
     [java] -----------------------------------------
     [java] 00201  100%  calculateEntitlement

However, if I change the test code to override the calculateEntitlement
method like this:

package foo;

import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;


public final class Main {
	public Main() {
		ApplicationContext ctx = new
ClassPathXmlApplicationContext("/beans.xml", Main.class);

        	EntitlementCalculationService
entitlementCalculationService = new EntitlementCalculationService(){
        		@Override
        		public void calculateEntitlement() {
        			System.out.println("test1");
        		}
        	};

        	entitlementCalculationService.calculateEntitlement();
	}

	public static void main(String[] args) {
		new Main();
    	}
}

The aspect fails to trigger.

Any ideas please?

TIA,

Adrian Smith
BT Group


Back to the top