Create a new Logging class:

I want the Java code methods of my business class to be able to execute some hook code before, after, after returning methods, and after throwing exceptions. The hook code is written in the Logging class’s corresponding methods:

  • beforeAdvice
  • afterAdvice
  • afterReturningAdvice
  • AfterThrowingAdvice

The business code is the Student Bean: in the mainapp. Java, two methods of the Student Bean are called: getName and getAge. I expect the four methods defined in the Logging class to be woven into AOP.

(1) Define aop’s namespace:

(2) Define an aspect where the ref attribute points to the logging Bean:

Pointcut: Join points that specify which pointcuts should be executed. The ID selectAll, combined with the expression expression attribute, indicates that the expected logging logic is woven into all advice and executed.

(3) Execute the beforeAdvice method of the logging bean before the methods of all beans under com.sap.*. Similarly, the afterAdvice method of the Logging bean is executed after the methods of all beans under com.sap.* are executed but not yet returned.

As observed from the call stack, Cglib also participates in the dynamic weaving process of the Pointcut.

For more of Jerry’s original articles, please follow the public account “Wang Zixi “: