2014-03-13 79 views
2

攔截方法調用這裏是彈簧xml文件的一部分:@AfterReturning接口失敗通過的AspectJ

public class FileService { 
public long read(Object obj,String id) throws Exception { 
    return 0L; 
    } 

public long write(Object obj,String id) throws Exception { 
    return 0L; 
    } 
} 


@Aspect 
public class ThroughputManager { 

public static long TOTAL_READ_THROUGHPUT; 
public static long TOTAL_WRITE_THROUGHPUT; 

@AfterReturning(
     pointcut="execution(* com.test.file.FileService.read(..))", 
     returning="size" 
     ) 
public void calculateReadThroughput(long size) throws IOException{ 
    TOTAL_READ_THROUGHPUT+=size; 
} 

@AfterReturning(
     pointcut="execution(* com.test.file.FileService.write(..))", 
     returning="size" 
     ) 
public void calculateWriteThroughput(long size) throws IOException{ 
    TOTAL_WRITE_THROUGHPUT+=size; 
} 
} 

當調試程序並調用:用Java代碼以下

<!-- aop config --> 
<aop:aspectj-autoproxy /> 
<bean id="fileService" class="com.test.file.FileService" /> 
<bean id="throughputManager" class="com.test.mbean.ThroughputManager" /> 

readwrite方法,ThroughputManager中的兩種方法未被調用。我試圖找到原因,但似乎關於代碼的一切都很好。任何人都可以幫助找出這個aop調用什麼錯誤?Thx。

+0

有什麼異常? –

+0

將代碼發佈到實際測試的位置。我的猜測是你沒有測試春季配置的實例,但正在構建一個新實例並將其用於測試。 –

+0

@ Deepak2221沒有例外。我知道如果引發異常,調用將失敗。但我在一個非例外情況下進行測試。所以..仍然沒有線索。 –

回答

1

我終於明白了。雖然我在spring配置xml中註冊了FileService作爲bean,但我沒有通過FileService bean - getBeans方法的實例調用readwrite方法。相反,我只是new一個FileService實例並調用這兩個方法。真是個大錯...