簡體   English   中英

Spring AOP @注釋

[英]Spring AOP @annotation

我是AOP的新手,尤其是Spring AOP。

我想以某種特定方法記錄執行時間。 我閱讀了Spring文檔,並認為最好的解決方案是使用注釋切入點創建方面。

看起來像:

@Around("@annotation(com.x.y.MethodExecutionTime)")
public Object methodExecutionTimeLog(ProceedingJoinPoint joinPoint) throws Throwable 
    StopWatch stopWatch = new StopWatch();

    Object retVal = null;
    try {
        stopWatch.start();
        retVal = joinPoint.proceed();
        stopWatch.stop();
        logger.info("Execution time: " + stopWatch.getTotalTimeMillis() + " ms");
    } catch(Throwable e) {
        logger.error("Execution time: " + stopWatch.getTotalTimeMillis() + " ms");
        throw e;
    }
    return retVal;
}

在方法中使用注釋:

    @Override
@MethodExecutionTime
public <T> T copy(Class<T> destType) {

    T t = ReflectionHelper.newInstance(destType);

    copyTo(t);

    return t;
}

Spring XML配置:

<context:spring-configured />
<aop:aspectj-autoproxy proxy-target-class="true" />

但它什么也不記錄。

我正在使用Spring 3.0.5

有任何想法嗎? 謝謝

如果其他所有配置都正確,那么它應該是Spring AOP的限制之一(至少是其默認配置),即:

  • 應用方面的對象應由Spring管理(即應從應用程序上下文中獲取,而不是使用new創建)

  • 調用應從該對象的“外部”發起,即,當您調用同一對象的另一種方法時,方面將不適用

  • <aop:aspectj-autoproxy>應該在與要應用方面的對象相同的應用程序上下文中聲明(尤其是在典型的Spring Web MVC應用applicationContext.xml...-servlet.xml形成不同的應用程序上下文)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM