Pergunta

I am trying to create an aspect to monitor the time execution of certain methods. I define the annotation as:

@Retention(RetentionPolicy.RUNTIME)
@Target(
{
    ElementType.METHOD, 
    ElementType.TYPE
})
public @interface TimePerformance {

}

And this is the aspect code:

@Around("execution(* *(..)) && @annotation(timePerformance)")
    public Object  timePerformance(ProceedingJoinPoint pjp,TimePerformance timePerformance) throws Throwable {

        if (LOG.isInfoEnabled()) {
             LOG.info("AOP - Before executing "+pjp.getSignature());
        }

        Long startTime = System.currentTimeMillis();

        Object result = pjp.proceed();

        Long stopTime = System.currentTimeMillis();

        LOG.info("MONITOR TIME_EXECUTION "+pjp.getSignature()+" : "+(stopTime-startTime));

        if (LOG.isInfoEnabled()) {
             LOG.info("AOP - After  executing "+pjp.getSignature());
        }

        return result;

    }

And the configuration is:

<!-- AOP support -->
<bean id='stateAspectImpl' class='eu.genetwister.snpaware.ui.aspect.StateAspectImpl' />
 <bean id='monitorImpl' class='eu.genetwister.snpaware.monitor.MonitorImpl' />
<aop:aspectj-autoproxy>
    <aop:include name='stateAspectImpl' />
     <aop:include name='monitorImpl' />
</aop:aspectj-autoproxy>

I have been annotated a method (part of a spring batch job) like this:

@BeforeStep
    @TimePerformance
    public void retrieveInterstepData(StepExecution stepExecution)

But the aspect is never being executed, even if the method is executed.

Does anyone an idea about how to solve this?

Thanks

Foi útil?

Solução

Why do you need the execution(..) in the pointcut expression? I believe @Around(value = @annotation(<full name of the class including the package>)") should work. Because, you are saying by using the annotation,that, whichever method is annotated with this annotation needs to be inspected and the @Around advice invoked.

You wouldnt need the execution expression in my opinion because, even if your custom annotation can be applied to fields, Spring AOP doesnt support Aspects on fields.

Also, do you need to bind the object in the aspect? You can also get the object from pjp.getArgs(); just in case.

EDIT : Here is the Aspect

@Component(value = "regionAccessValidatorAspect")
@Aspect
public class RegionAccessValidatorAspect {

   @Around(value = "@annotation(com.....RegionAccessValidator)")
   public Object doAccessCheck(final ProceedingJoinPoint jp) throws Throwable {

..... }

Here is the processor on which i have setup my annotation

   @RegionAccessValidator(getVal = "Temporary")
   public CountryProductOfferingRepresentation update(final CountryProductOfferingRepresentation cpoRep,
         final RequestType requestType) throws Exception {

Here is the annotation

@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface RegionAccessValidator {

   String getVal();

}

Although, i havent tried by passing it an argument, i will try that today and find out if that is the cause.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top