Interceptor
Interceptor can intercept methods and provide the opportunity to add aspect code before and after the method to achieve the core goal of AOP. The Interceptor interface only defines one method, void intercept(Invocation inv). The following is a simple example:
public class DemoInterceptor implements Interceptor { public void intercept(Invocation inv) { System.out.println("Before method invoking"); inv.invoke(); System.out.println("After method invoking"); } }
The DemoInterceptor in the above code will intercept the target method and forward the console before and after the target method is called. Output text. The line of code inv.invoke() is a call to the target method. AOP can be easily implemented by inserting aspect code before and after this line of code.
nvocation, as the only parameter in the intercept method of the Interceptor interface, provides many convenient methods for use in the interceptor. The following are the methods in Invocation: