1.前置通知 Before advice
Advice that executes before a join point, but which does not have the ability to prevent execution flow proceeding to the join point (unless it throws an exception).
2.后置通知 After (finally) advice
Advice to be executed regardless of the means by which a join point exits (normal or exceptional return).
3.返回通知 After returningadvice
Advice to be executed after a join point completes normally: for example, if a method returns without throwing an exception.
4.异常通知 After throwing advice
Advice to be executed if a method exits by throwing an exception.
5.环绕通知 Around advice
Advice that surrounds a join point such as a method invocation. This is the most powerful kind of advice. Around advice can perform custom behavior before and after the method invocation. It is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception.
一.前置通知
Before advice is declared in an aspect using the@Before
annotation:
1 importorg.aspectj.lang.annotation.Aspect; 2 importorg.aspectj.lang.annotation.Before; 3 4 @Aspect 5 public classBeforeExample { 6 7 @Before("com.xyz.myapp.SystemArchitecture.dataAccessOperation()") 8 public voiddoAccessCheck() { 9 //... 10 } 11 12 }
If using an in-place pointcut expression we could rewrite the above example as:
1 importorg.aspectj.lang.annotation.Aspect; 2 importorg.aspectj.lang.annotation.Before; 3 4 @Aspect 5 public classBeforeExample { 6 7 @Before("execution(* com.xyz.myapp.dao.*.*(..))") 8 public voiddoAccessCheck() { 9 //... 10 } 11 12 }
二.后置通知
After (finally) advice runs however a matched method execution exits. It is declared using the@After
annotation. After advice must be prepared to handle both normal and exception return conditions. It is typically used for releasing resources, etc.
importorg.aspectj.lang.annotation.Aspect; importorg.aspectj.lang.annotation.After; @Aspect public classAfterFinallyExample { @After("com.xyz.myapp.SystemArchitecture.dataAccessOperation()") public voiddoReleaseLock() { //... } }
三.返回通知
After returning advice runs when a matched method execution returns normally. It is declared using the@AfterReturning
annotation:
importorg.aspectj.lang.annotation.Aspect; importorg.aspectj.lang.annotation.AfterReturning; @Aspect public classAfterReturningExample { @AfterReturning("com.xyz.myapp.SystemArchitecture.dataAccessOperation()") public voiddoAccessCheck() { //... } }
Sometimes you need access in the advice body to the actual value that was returned. You can use the form of@AfterReturning
that binds the return value for this:
1 importorg.aspectj.lang.annotation.Aspect; 2 importorg.aspectj.lang.annotation.AfterReturning; 3 4 @Aspect 5 public classAfterReturningExample { 6 7 @AfterReturning( 8 pointcut="com.xyz.myapp.SystemArchitecture.dataAccessOperation()", 9 returning="retVal") 10 public voiddoAccessCheck(Object retVal) { 11 //... 12 } 13 14 }
四.异常通知
After throwing advice runs when a matched method execution exits by throwing an exception. It is declared using the@AfterThrowing
annotation:
importorg.aspectj.lang.annotation.Aspect; importorg.aspectj.lang.annotation.AfterThrowing; @Aspect public classAfterThrowingExample { @AfterThrowing("com.xyz.myapp.SystemArchitecture.dataAccessOperation()") public voiddoRecoveryActions() { //... } }
Often you want the advice to run only when exceptions of a given type are thrown, and you also often need access to the thrown exception in the advice body. Use thethrowing
attribute to both restrict matching (if desired, useThrowable
as the exception type otherwise) and bind the thrown exception to an advice parameter.
importorg.aspectj.lang.annotation.Aspect; importorg.aspectj.lang.annotation.AfterThrowing; @Aspect public classAfterThrowingExample { @AfterThrowing( pointcut="com.xyz.myapp.SystemArchitecture.dataAccessOperation()", throwing="ex") public voiddoRecoveryActions(DataAccessException ex) { //... } }
五.环绕通知
The final kind of advice is around advice. Around advice runs "around" a matched method execution. It has the opportunity to do work both before and after the method executes, and to determine when, how, and even if, the method actually gets to execute at all. Around advice is often used if you need to share state before and after a method execution in a thread-safe manner (starting and stopping a timer for example). Always use the least powerful form of advice that meets your requirements (i.e. don’t use around advice if simple before advice would do).
Around advice is declared using the@Around
annotation. The first parameter of the advice method must be of typeProceedingJoinPoint
. Within the body of the advice, callingproceed()
on theProceedingJoinPoint
causes the underlying method to execute. Theproceed
method may also be called passing in anObject[]
- the values in the array will be used as the arguments to the method execution when it proceeds.
importorg.aspectj.lang.annotation.Aspect; importorg.aspectj.lang.annotation.Around; importorg.aspectj.lang.ProceedingJoinPoint; @Aspect public classAroundExample { @Around("com.xyz.myapp.SystemArchitecture.businessService()") public Object doBasicProfiling(ProceedingJoinPoint pjp) throwsThrowable { //start stopwatch Object retVal =pjp.proceed(); //stop stopwatch returnretVal; } }