2011-05-05 57 views
0

我試圖寫一個切入點和建議可能從以下方法打印字符串的變量 -AspectJ切入點內省本地方法的代碼,並打印內部本地方法

public CustomerDto getCustomer(Integer customerCode){   
      CustomerDto customerDto = new CustomerDto();   
      String emailID =getEmailAddress(); 
      customerDto.setEmailAddress(emailID);    
      customerDto.setEmployer(getEmployer()); 
      customerDto.setSpouseName(getSpouse()); 
      return customerDto;  
} 

我無法找出一個切入點查看字符串emailID的方式,然後在建議中打印相同的值。

+0

我不知道你正在嘗試做的。您是否在執行getCustomer時試圖執行建議? – 2011-05-05 18:32:31

回答

2

也許你需要像下面這樣:

public privileged aspect LocalMethodCallAspect { 
    private pointcut localMethodExecution() : withincode(public CustomerDto TargetClass.getCustomer(Integer)) && 
     call(private String TargetClass.getEmailAddress()); 

    after() returning(String email) : localMethodExecution() 
    { 
     System.out.println(email); 
    } 
} 

哪裏TargetClass是包含getCustomer()getEmailAddress()方法的類。

或者相同的使用@AspectJ:

@Aspect 
public class LocalMethodCallAnnotationDrivenAspect { 
    @Pointcut("withincode(public CustomerDto TargetClass.getCustomer(Integer)) && " + 
      "call(private String TargetClass.getEmailAddress())") 
    private void localMethodExecution() { 

    } 

    @AfterReturning(pointcut="localMethodExecution()",returning="email") 
    public void printingEmail(String email) { 
     System.out.println(email); 
    } 
} 
+0

我知道這是一個老問題,但我無法使@AspectJ部分工作。 當我使用'withincode'時,我得到錯誤'Pointcut表達式'localMethodExecution()'包含不支持的切入點基元'withincode'' 當我嘗試'@ withincode'時出現錯誤'切入點不正確:期待')'在字符位置19 @withincode(public int com .....' 我使用AspectJ註釋,儘管在Spring文檔中它說_Spring_不支持'withincode'和'@ withincode' .. 。是這樣的原因嗎? – Roger 2013-06-04 09:17:34

+0

@Roger提出新問題。 – 2015-03-05 06:07:25