Ở đây, chúng tôi sẽ sử dụng các ví dụ về kiểm toán để chứng minh làm thế nào để lựa chọn các biện pháp với các chú thích và phương pháp trong các lớp học với các chú thích. Trong suốt ví dụ này, chúng tôi sẽ sử dụng các chú thích kiểm toán @ thể hiện trong bảng liệt kê 4-45. Ví dụ 4-45. @ Kiểm toán gói | 126 CHAPTER 4 SPRING AOP Here we ll use the example of auditing to demonstrate how to select methods with annotations and methods in classes with annotations. Throughout this example we ll use the @Audit annotation shown in Listing 4-45. Listing 4-45. The @AuditAnnotation package import import @Retention public interface Audit We can now use this annotation on the BusinessOperations class in shown Listing 4-46. Listing 4-46. Using the @Audit Annotation to Mark a Method That Requires Retention of Audit Information package public class BusinessOperations @Audit public void sensitiveOperation long recordId . By declaring the @Audit annotation on the sensitiveOperation method we ve indicated we need to retain audit information for this method. We haven t determined however which information to retain where to store this information and for how long. The information that is to be stored and how it is to be stored is very likely defined in a policy document and is equal for all methods. We ve only marked a method and can now implement the retention policy with an aspect. Selecting on Method Annotation Declarations We need to write an aspect that implements the audit retention policy. Selecting which methods are subject to audit information retention is a separate effort and of no concern to the aspect or the developers who implement it. Because we can base the pointcut on a Java 5 annotation we get finegrained semantics. We will select only the methods that declare the @Audit annotation and leave other methods unaffected. This is a great way of working since we can be sure there will be no side effects. We ll delegate the actual saving of audit information to the AuditInformationRetentionPolicy interface as shown in Listing 4-47. Listing 4-47. TheAuditInformationRetentionPolicy Interface package .