我正在尝试编写一个切入点和建议,可以从以下方法打印字符串 -
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的方法,然后在建议中打印相同的值。
答案 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);
}
}