spring aop切面执行顺序


spring aop切面执行顺序

     

             

                              

切面执行顺序

       

现有切面1、切面2对同一个切点按先后顺序执行(切面1先于切面2执行)

切面1:@Before、@After、@AfterReturnning、@AfterThrowing、
      @Around 执行前、@Around 正常执行、@Around 异常执行、@Around 执行后

切面2:@Before、@After、@AfterReturnning、@AfterThrowing、
      @Around 执行前、@Around 正常执行、@Around 异常执行、@Around 执行后

         

切点正常执行

# 切点执行前
切面1:@Around 执行前、@Before
切面2:@Around 执行前、@Before

切点执行

# 切点执行后
切面2:@AfterReturning、@After、@Around 正常执行、@Around 执行后
切面1:@AfterReturning、@After、@Around 正常执行、@Around 执行后

      

切点异常执行

# 切点执行前
切面1:@Around 执行前、@Before
切面2:@Around 执行前、@Before

切点执行

# 切点执行后
切面2:@AfterThrowing、@After、@Around 异常执行、@Around 执行后
切面1:@AfterThrowing、@After、@Around 异常执行、@Around 执行后

       

              

                              

使用示例

            

                          spring aop切面执行顺序_第1张图片

            

HelloService

public interface HelloService {

    String hello();
}

      

HelloServiceImpl

@Service
public class HelloServiceImpl implements HelloService {

    @Override
    public String hello() {
        return "hello";
    }
}

       

CustomAspect

@Aspect
@Order(-2)
@Component
public class CustomAspect {

    @Pointcut("execution(* *.hello()) && within(com.example.demo.service..*)")
    public void fun(){

    }

    @Before("fun()")
    public void before(){
        System.out.println("CustomAspect before");
    }

    @After("fun()")
    public void after(){
        System.out.println("CustomAspect after");
    }

    @AfterReturning("fun()")
    public void afterReturning(){
        System.out.println("CustomAspect afterReturning");
    }

    @AfterThrowing("fun()")
    public void afterThrowing(){
        System.out.println("CustomAspect afterThrowing");
    }

    @Around("fun()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
        Object result = null;

        System.out.println("CustomAspect around执行前");
        try {
            result = joinPoint.proceed();
            System.out.println("CustomAspect around执行后正常返回");
        }catch (Throwable e){
            e.printStackTrace();
            System.out.println("CustomAspect around执行后抛出异常");

            throw e;
        }finally {
            System.out.println("CustomAspect around执行后");
        }

        return result;
    }
}

        

CustomAspect2

@Aspect
@Order(-1)
@Component
public class CustomAspect2 {

    @Pointcut("execution(* *.hello()) && within(com.example.demo.service..*)")
    public void fun(){

    }

    @Before("fun()")
    public void before(){
        System.out.println("CustomAspect2 before");
    }

    @After("fun()")
    public void after(){
        System.out.println("CustomAspect2 after");
    }

    @AfterReturning("fun()")
    public void afterReturning(){
        System.out.println("CustomAspect2 afterReturning");
    }

    @AfterThrowing("fun()")
    public void afterThrowing(){
        System.out.println("CustomAspect2 afterThrowing");
    }

    @Around("fun()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
        Object result = null;

        System.out.println("CustomAspect2 around执行前");
        try {
            result = joinPoint.proceed();
            System.out.println("CustomAspect2 around执行后正常返回");
        }catch (Throwable e){
            e.printStackTrace();
            System.out.println("CustomAspect2 around执行后抛出异常");

            throw e;
        }finally {
            System.out.println("CustomAspect2 around执行后");
        }

        return result;
    }
}

      

HelloController

@RestController
public class HelloController {

    @Resource
    private HelloService helloService;

    @RequestMapping("/hello")
    public String hello(){
        return helloService.hello();
    }
}

       

            

                              

使用测试

   

localhost:8080/hello,控制台输出:

CustomAspect around执行前
CustomAspect before
CustomAspect2 around执行前
CustomAspect2 before
CustomAspect2 afterReturning
CustomAspect2 after
CustomAspect2 around执行后正常返回
CustomAspect2 around执行后
CustomAspect afterReturning
CustomAspect after
CustomAspect around执行后正常返回
CustomAspect around执行后

               

                      

你可能感兴趣的:(spring,aop,spring,aop)