AspectJ Pointcut для интроспекции локального кода метода и печати переменной внутри локального метода

Я пытаюсь написать pointcut и совет, который может напечатать строку из следующего метода -

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

Я не могу выяснить, каким образом pointcut смотрит на электронный идентификатор String, а затем выводит его значение в рекомендации.

1 ответ

Решение

Может быть, вам нужно что-то вроде следующего:

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);
    }
}
Другие вопросы по тегам