Judking
Judking

Reputation: 6381

how to associate one aspect method with multiple methods in aspectj?

I have a class A:

@Service
public class A {
    public void goX()    {
        System.out.println("goX");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void goY()    {
        System.out.println("goY");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

and an AOP class AOP:

@Aspect
@Service
class AOP        {
    @Around("execution(* com.test.A.goX(..))")
    public void calExecTime(ProceedingJoinPoint proceedingJoinPoint) throws Throwable        {
        long t1 = System.currentTimeMillis();
        proceedingJoinPoint.proceed();
        long t2 = System.currentTimeMillis();
        System.out.println(t2-t1);
    }
}

Then I can count the time A.goX() takes to execute by method AOP.calExecTime().

What I want is to calculate the time for both A.goX() and A.goY() by the same method AOP.calExecTime(), I don't know how to write things in @Around annotation. Could anyone help me out? Thanks a lot.

Upvotes: 2

Views: 1999

Answers (1)

Ashok kumar
Ashok kumar

Reputation: 435

This could help.

@Aspect
@Service
class AOP        {
@Around("within(* com.test.*)")
public void calExecTime(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    long t1 = System.currentTimeMillis();
    proceedingJoinPoint.proceed();
    long t2 = System.currentTimeMillis();
    System.out.println("Method "+ proceedingJoinPoint.getSignature().getName() + " time : "+  t2-t1);

    }

   }

Upvotes: 3

Related Questions