AspectJ Pointcut内省本地方法代码并在本地方法中打印变量

时间:2011-05-05 18:10:37

标签: aspectj pointcuts

我正在尝试编写一个切入点和建议,可以从以下方法打印字符串 -

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

我无法找出切入点查看String emailID的方法,然后在建议中打印相同的值。

1 个答案:

答案 0 :(得分:4)

也许您需要以下内容:

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);
    }
}