Spring AOP is often used in daily development, and the types of notifications are: Around, Before, After, AfterReturning and AfterThrowing. This paper mainly explains the execution order of five kinds of notification under normal and abnormal conditions, and what is the execution order when multiple sections exist

Normal logical code

  • The interface is defined as follows
public interface UserService {

    void saveUser(a);

    void deleteUser(a);
}
Copy the code
  • The interface implementation code is as follows
@Slf4j
@Component
public class UserServiceImpl implements UserService {

    @Override
    public void saveUser(a) {
        log.info("save user");
    }

    @Override
    public void deleteUser(a) {
        log.info("delete user");
        System.out.println(1 / 0); }}Copy the code

For normal interfacesUserServiceDefine two aspects of logic

  • Section a
@Slf4j
@Order(99)// Pay attention to the order
@Aspect
@Component
public class UserServiceAspect {

    @Before("execution(* com.lushwe.aspect.service.impl.. * (..) )")
    public void before(a) {
        log.info("before");
    }

    @Around("execution(* com.lushwe.aspect.service.impl.. * (..) )")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        log.info("Went around");
        Object proceed = proceedingJoinPoint.proceed();
        log.info("Around the end");
        return proceed;
    }

    @After("execution(* com.lushwe.aspect.service.impl.. * (..) )")
    public void after(a) {
        log.info("after");
    }

    @AfterReturning("execution(* com.lushwe.aspect.service.impl.. * (..) )")
    public void afterReturning(a) {
        log.info("afterReturning");
    }

    @AfterThrowing("execution(* com.lushwe.aspect.service.impl.. * (..) )")
    public void afterThrowing(a) {
        log.info("afterThrowing"); }}Copy the code
  • Section 2
@Slf4j
@Order(100)// Pay attention to the order
@Aspect
@Component
public class UserServiceAspectTwo {

    @Before("execution(* com.lushwe.aspect.service.impl.. * (..) )")
    public void before(a) {
        log.info("before");
    }

    @Around("execution(* com.lushwe.aspect.service.impl.. * (..) )")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        log.info("Went around");
        Object proceed = proceedingJoinPoint.proceed();
        log.info("Around the end");
        return proceed;
    }

    @After("execution(* com.lushwe.aspect.service.impl.. * (..) )")
    public void after(a) {
        log.info("after");
    }

    @AfterReturning("execution(* com.lushwe.aspect.service.impl.. * (..) )")
    public void afterReturning(a) {
        log.info("afterReturning");
    }

    @AfterThrowing("execution(* com.lushwe.aspect.service.impl.. * (..) )")
    public void afterThrowing(a) {
        log.info("afterThrowing"); }}Copy the code

Normally, the execution sequence is as follows

The 2020-04-17 15:27:55. 4329-500 the INFO [main] com. Lushwe. Aspect. UserServiceAspect: Around to the 2020-04-17 15:27:55. 501 INFO 4329 - [the main] com. Lushwe. Aspect. UserServiceAspect: Before the 2020-04-17 15:27:55. 501 INFO 4329 - [the main] com. Lushwe. Aspect. UserServiceAspectTwo: Around to the 2020-04-17 15:27:55. 501 INFO 4329 - [the main] com. Lushwe. Aspect. UserServiceAspectTwo: Before the 2020-04-17 15:27:55. 501 INFO 4329 - [the main] C.L.A spect. Service. Impl. UserServiceImpl: Save the user the 2020-04-17 15:27:55. 503 INFO 4329 - [the main] com. Lushwe. Aspect. UserServiceAspectTwo: Around the end of the 2020-04-17 15:27:55. 503 INFO 4329 - [the main] com. Lushwe. Aspect. UserServiceAspectTwo: After the 2020-04-17 15:27:55. 503 INFO 4329 - [the main] com. Lushwe. Aspect. UserServiceAspectTwo: AfterReturning 15:27:55 2020-04-17. 4329-503 the INFO [main]. Com lushwe. Aspect. UserServiceAspect: Around the end of the 2020-04-17 15:27:55. 503 INFO 4329 - [the main] com. Lushwe. Aspect. UserServiceAspect: After the 2020-04-17 15:27:55. 503 INFO 4329 - [the main] com. Lushwe. Aspect. UserServiceAspect: afterReturningCopy the code

If there is an exception, the log is displayed as follows

The 2020-04-17 15:20:55. 4227-403 the INFO [main] com. Lushwe. Aspect. UserServiceAspect: Around to the 2020-04-17 15:20:55. 403 INFO 4227 - [the main] com. Lushwe. Aspect. UserServiceAspect: Before the 2020-04-17 15:20:55. 403 INFO 4227 - [the main] com. Lushwe. Aspect. UserServiceAspectTwo: Around to the 2020-04-17 15:20:55. 403 INFO 4227 - [the main] com. Lushwe. Aspect. UserServiceAspectTwo: Before the 2020-04-17 15:20:55. 403 INFO 4227 - [the main] C.L.A spect. Service. Impl. UserServiceImpl: Delete the user the 2020-04-17 15:20:55. 403 INFO 4227 - [the main] com. Lushwe. Aspect. UserServiceAspectTwo: After the 2020-04-17 15:20:55. 405 INFO 4227 - [the main] com. Lushwe. Aspect. UserServiceAspectTwo: AfterThrowing 15:20:55 2020-04-17. 4227-405 the INFO [main]. Com lushwe. Aspect. UserServiceAspect: After the 2020-04-17 15:20:55. 406 INFO 4227 - [the main] com. Lushwe. Aspect. UserServiceAspect: afterThrowing Exceptionin thread "main"java.lang.ArithmeticException: / by zero ... (Exception details omitted)...Copy the code

conclusion

  • Normal execution order:Around start -> Before -> Around end -> After -> AfterReturning
  • Order of execution in case of exception:Around start -> Before -> After -> AfterThrowing, when there is an exception,AroundThe end logic will not execute
  • When there are many facets, follow@OrderExecute them in sequence, as can be seen from the above log

  • The end of this article, I hope to help you