Đoạn code sau in ra gì?
@Target(ElementType.METHOD)
@interface Audited {}
class PaymentService {
@Audited
public void pay() {}
}
Method m = PaymentService.class.getMethod("pay");
System.out.println(m.isAnnotationPresent(Audited.class));
Xem đáp án và giải thích
Đây là lỗi kinh điển khi tự viết annotation cho AOP hoặc interceptor: quên @Retention(RetentionPolicy.RUNTIME) khiến framework "không thấy" annotation và logic không chạy mà không có lỗi gì. Retention mặc định là CLASS. Annotation dùng với reflection hay Spring AOP luôn cần RUNTIME.