In the previous blog article "xml configuration of Spring Aop instances", we explained the xml configuration method. Today we will talk about configuring spring aop Using the AspectJ annotation method.
For more information about the jdk proxy and interface and implementation code, see the previous blog. I will share with you the Aspect class:
Package com. tgb. aop; import org. aspectj. lang. joinPoint; import org. aspectj. lang. proceedingJoinPoint; import org. aspectj. lang. annotation. after; import org. aspectj. lang. annotation. afterReturning; import org. aspectj. lang. annotation. afterThrowing; import org. aspectj. lang. annotation. around; import org. aspectj. lang. annotation. aspect; import org. aspectj. lang. annotation. before; import org. aspectj. lang. annotation. de ClareParents; import org. aspectj. lang. annotation. pointcut;/*** test after, before, und, throwing, returning Advice. * @ author Admin **/@ Aspectpublic class AspceJAdvice {/*** Pointcut * defines Pointcut. The Pointcut name is aspectjMethod (), this method has no return value and parameter * This method is an identifier and does not call */@ Pointcut (execution (* find *(..))) private void aspectjMethod () {};/*** Before * is executed Before the core business is executed, and cannot block calls from the core business. * @ Param joinPoint */@ Before (aspectjMethod () public void beforeAdvice (JoinPoint joinPoint) {System. out. println (----- beforeAdvice (). invoke -----); System. out. println (this is to make some security judgments before executing the core business logic); System. out. println (the required content can be obtained through joinPoint); System. out. println (----- End of beforeAdvice () ------);}/*** After * After the core business logic exits (including normal execution ends and abnormal exit ), run this Advice * @ param joinPoint */@ After (value = aspectjMethod () public void afterAdvice (JoinPoint joinPoint) {System. out. println (----- afterAdvice (). invoke -----); System. out. println (here, some logging operations are performed after the core business logic is executed); System. out. println (the required content can be obtained through joinPoint); System. out. println (----- End of afterAdvice () ------);}/***** Around * manually controls the core business logic of the call and the processing before and after the call. ** note: after an exception is thrown to the core business, exit immediately and switch to AfterAdvice * To Run AfterAdvice, then go to ThrowingAdvice * @ param pjp * @ return * @ throws Throwable */@ Around (value = aspectjMethod () public Object aroundAdvice (ProceedingJoinPoint pjp) throws Throwable {System. out. println (----- aroundAdvice (). invoke -----); System. out. println (here we can do something similar to Before Advice); // call the core logic Object retVal = pjp. proceed (); System. out. println (here we can do something similar to After Advice); System. out. println (----- End of aroundAdvice () ------); return retVal;}/*** AfterReturning * after the core business logic call Exits normally, no matter whether the return value exists or not, run this Advice * @ param joinPoint */@ AfterReturning (value = aspectjMethod (), returning = retVal) public void afterReturningAdvice (JoinPoint joinPoint, String retVal) {System. out. println (----- afterReturningAdvice (). invoke -----); System. out. println (Return Value: + retVal); System. out. println (the return value can be further processed here); System. out. println (the required content can be obtained through joinPoint); System. out. println (----- End of afterReturningAdvice () ------);}/*** after the core business logic call exits unexpectedly, execute this Advice to handle error messages ** note: the execution order is after round Advice * @ param joinPoint * @ param ex */@ AfterThrowing (value = aspectjMethod (), throwing = ex) public void afterThrowingAdvice (JoinPoint joinPoint, Exception ex) {System. out. println (----- afterThrowingAdvice (). invoke -----); System. out. println (error message: + ex. getMessage (); System. out. println (this is intended to capture exceptions when an error occurs in the core business logic and do some logging operations); System. out. println (the required content can be obtained through joinPoint); System. out. println (----- End of afterThrowingAdvice ()------);}}
In the application-config.xml, you only need to configure the business logic bean and Aspect bean and enable Aspect annotation: