2017-05-26 1 views
0

Le problème est @Before et @AfterReturning fonctionnent mais ce n'est pas le cas pour Pointcut.Point de coupure de point de départ non déclenché

Voici mon aspect.

Dans le cadre d'un service springboot, ce que je veux faire est de déclencher le pointcut avec la première méthode profil pour afficher le temps d'exécution et d'autres choses.

Ai-je raté quelque chose?

package com.myproj.service.myagg.aop; 

import org.aspectj.lang.JoinPoint; 
import org.aspectj.lang.ProceedingJoinPoint; 
import org.aspectj.lang.annotation.AfterReturning; 
import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.Before; 
import org.aspectj.lang.annotation.Pointcut; 
import org.springframework.stereotype.Component; 
import org.springframework.util.StopWatch; 

/** 
* Created by shammami on 26/05/2017. 
*/ 
@Aspect 
@Component 
public class LoggingService { 

    @Pointcut("execution(public void com.myproj.service.myagg.listener.MyMessageConsumer.handleMessage(..))") 
    public Object profile(ProceedingJoinPoint pjp) throws Throwable { 
     StopWatch stopWatch = new StopWatch(); 
     stopWatch.start(); 
     boolean isExceptionThrown = false; 
     try { 
      // execute the profiled method 
      return pjp.proceed(); 
     } catch (RuntimeException e) { 
      isExceptionThrown = true; 
      throw e; 
     } finally { 
      stopWatch.stop(); 
      StopWatch.TaskInfo taskInfo = stopWatch.getLastTaskInfo(); 
      // Log the method's profiling result 
      String profileMessage = taskInfo.getTaskName() + ": " + taskInfo.getTimeMillis() + " ms" + 
        (isExceptionThrown ? " (thrown Exception)" : ""); 
      System.out.println(profileMessage); 
     } 
    } 

    @Before("execution(public void com.myproj.service.myagg.listener.MyMessageConsumer.handleMessage(..))") 
    public void before(JoinPoint joinPoint) { 
     System.out.println("Started: " + joinPoint.getStaticPart().getSignature().toLongString()); 
    } 

    @AfterReturning("execution(public void com.myproj.service.myagg.listener.MyMessageConsumer.handleMessage(..))") 
    public void completed(JoinPoint joinPoint) { 
     System.out.println("Completed: " + joinPoint.getStaticPart().getSignature().toLongString()); 
    } 
} 

Répondre

0

Lorsque vous annoter quelque chose avec @Pointcut vous définissez essentiellement la signature de pointcut, vous ne pouvez pas faire toute sorte de traitement là-dedans. Ce que vous devez faire est de créer une autre méthode qui a tous les détails de traitement et utilise la signature pointcut que vous avez évalué ci-dessus. Par conséquent,

@Pointcut("execution(public void com.myproj.service.myagg.listener.MyMessageConsumer.handleMessage(..))") 
public void myPointcutSignature(){ 
//This basically has nothing :) 
} 

@Around("myPointcutSignature") 
public Object profile(ProceedingJoinPoint pjp) throws Throwable { 
    StopWatch stopWatch = new StopWatch(); 
    stopWatch.start(); 
    boolean isExceptionThrown = false; 
    //And the remaining code 
    ------- 
} 

Espérons que cela fonctionne. Souvenez-vous également que ProceedingJoinPoint peut être utilisé uniquement avec les conseils de @Around.

+0

Merci beaucoup chinmay! Ça fonctionne maintenant ! – Sofiane