2012-06-18 186 views
0

我是AOP的新手,特別是Spring AOP。Spring AOP @annotation

我想用某種特定的方法記錄執行時間。我閱讀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

任何想法?由於

+0

你能後的代碼使用這個註解?你可以發佈Spring Spring AOP的設置嗎? –

+1

您是否在春季xml中啓用了aop?顯示與aop相關的xml spring配置的一部分。 –

+0

我發佈了它,是否有必要添加一些其他選項到Spring XML配置文件? – vlcik

回答

1

如果一切配置正確,應該Spring AOP的侷限性之一(其默認配置至少,),即:

  • 對象應用方面應加以管理由Spring

  • 調用應該從「外部」該對象的起源(即應該從應用程序上下文,而不是與new創建獲得),即當你調用同一個對象

    的另一種方法方面不應用
  • <aop:aspectj-autoproxy>應爲對象相同的應用程序上下文中聲明應用方面(特別是在典型的Spring Web MVC應用程序applicationContext.xml...-servlet.xml形式不同的應用程序上下文)

+0

嗨axtavt,你可以請檢查我的帖子上Spring AOP - http://stackoverflow.com/questions/31984258/basic-aop-program-throws-beancurrentlyincreationexception – user3181365