Pointcut expression là gì?

pointcut@Pointcut Spring AOP & proxy

Biểu thức chọn các join point (method) mà advice áp dụng, ví dụ `execution(* com.shop.service..*.*(..))` hay `@annotation(com.shop.Audited)`.

EN: An expression selecting the join points (methods) an advice applies to, such as `execution(* com.shop.service..*.*(..))` or `@annotation(com.shop.Audited)`.

Thử một câu hỏi phỏng vấn

Câu 1 Dễ Một đáp án

Bạn tạo annotation @Audited (có @Retention(RetentionPolicy.RUNTIME)) để đánh dấu các method service cần ghi audit. Pointcut nào chọn đúng các method có gắn annotation này?

  1. A
    within(com.shop.audit.Audited) within nhận một pattern kiểu (class), không phải annotation trên method.
  2. B
    @annotation(com.shop.audit.Audited) Đúng: @annotation khớp các method mà bản thân method có gắn annotation đó.
  3. C
    @within(com.shop.audit.Audited) @within khớp mọi method trong class có gắn annotation ở mức class, không xét annotation trên method.
  4. D
    args(com.shop.audit.Audited) args khớp theo kiểu của tham số truyền vào lúc chạy, không liên quan đến annotation của method.
Xem đáp án và giải thích

Annotation tự định nghĩa cộng với @annotation(...) là cách phổ biến nhất để làm aspect "opt-in": chỉ method nào được gắn mới bị ảnh hưởng. Có thể gắn annotation vào tham số advice để đọc thuộc tính, ví dụ @Around("@annotation(audited)") với tham số Audited audited. Nhớ @Retention(RUNTIME), nếu không Spring AOP không đọc được annotation bằng reflection.

7 câu hỏi dùng thuật ngữ Pointcut expression
Luyện hết trong ứng dụng, có chấm điểm và lưu câu sai.

Đăng ký và luyện

Thuật ngữ cùng chủ đề

Nhớ lâu hơn bằng cách luyện

Đọc định nghĩa thì dễ quên, trả lời câu hỏi thì nhớ lâu. Thử ngay, miễn phí.

Đăng ký miễn phí