Adding class Libraries: Aspectjrt.jar and Aspectjweaver.jar
Add an AOP schema.
Define XML element:<aop:aspectj-autoproxy>
Write Java classes and use @aspect annotations as notifications
AspectJ supports 5 types of notification annotations:
@Before: Pre-notification, executing before method execution
@After: Post notification, executing after the method is executed
@AfterReturning: Returns a notification that executes after the method returns the result
@AfterThrowing: Exception notification, after the method throws an exception
@Around: Surround notifications, executing around methods
You can configure it as a normal bean element.
Two
Post notification: @After
@After ("Execution" (* *). welcomeservice.* (..)) ")
public void Applaud () {.}
The post notification is executed after the target method execution completes. A slice aspect contains many notices.
@After
A post notification indicates that the target method is finished, and the notification is woven, regardless of whether the exception is thrown or not.
@AfterReturning
After the method returns, the notification executes only after the target method returns, if the exception is not executed.
@AfterReturning (pointcut= "", returning= "res")
public void xxx (joinput jp,object res)
In afterreturning
The return value can be received in the notification. Res is the object used to receive the return value.
Three
Surround Notification: @Around
@Around ("Execution" (* *). welcomeservice.* (..)) ")
public void Around (Proceedingpointcut JP) {.}
Note: You can control whether the target method is invoked and return a completely different object, and use caution.
Specify the priority level:
@Aspect
@Order (0)
public class xxx{...}
Add a @order annotation to specify the priority of the slice to be added (in order, the smaller the value, the higher the priority)