2014-01-22 51 views
0

我想創建一個方面來監視某些方法的時間執行。我定義註解爲:方法註釋不被執行

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

} 

而這方面的代碼:

@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; 

    } 

而配置是:

<!-- 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> 

我已經被註釋的方法(春季批量工作的一部分)像這樣:

@BeforeStep 
    @TimePerformance 
    public void retrieveInterstepData(StepExecution stepExecution) 

但是,即使該方法被執行,該方面也不會被執行。

有沒有人有關如何解決這個問題的想法?

感謝

+0

爲什麼你需要在切入點表達式中執行(..)?我相信@Around(value = @annotation(<類的全名,包括包>))應該工作。 – Hrishikesh

+0

使用你的方法@Around(「value = @annotation(.TimePerformance)」)給出了關於錯誤格式爲 – ftrujillo

+0

你能提一下這個異常嗎?它是什麼意思?它可能是Aspect不是一個組件或一個bean – Hrishikesh

回答

1

爲什麼你需要切入點表達式的execution(..)? 我相信@Around(value = @annotation(<full name of the class including the package>)")應該工作。 因爲,您使用註釋來說,無論使用此註釋註釋哪種方法都需要檢查並調用@Around通知。

在我看來,你不需要執行表達式,因爲即使自定義註釋可以應用於字段,Spring AOP也不支持字段的Aspects。

另外,你是否需要綁定對象的方面?以防萬一,您也可以從pjp.getArgs();中獲取對象。

編輯: 這裏是看點

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

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

..... }

這裏是我已經設置我的註釋

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

這裏的處理器是註釋

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

    String getVal(); 

} 

雖然,我沒有嘗試通過它傳遞一個參數,我今天會嘗試,並找出是否是原因。

+0

正如我在評論中說的,使用你的語法導致一個異常fromat錯誤 – ftrujillo

+0

我不neet的對象,但對象是需要避免的異常導致:java.lang.IllegalArgumentException:錯誤在:: 0不能找到引用的切入點註釋 – ftrujillo

+0

我剛剛嘗試了一個@Around建議,其設置與您的設置類似,對我來說它的工作原理非常好。我在你的問題中注意到的是,你的切入點表達式中的(和*之間沒有空格,它應該是執行的(*,如果你使用的執行表達式是 – Hrishikesh

相關問題