Sping AOP实践

编写服务接口

public interface HelloService {

    String hello(String name);
}

@Service
public class HelloServiceImpl implements HelloService {
    @Override
    public String hello(String name) {
        if (name == null) {
            throw new RuntimeException("name must not empty!");
        }
        System.out.println("HelloServiceImpl hello() ==> " + name);
        return name;
    }
}

编写一个切面

@Component
@Aspect
class MyAspect01 {

    @Pointcut("execution(* com.example.springboot.service.HelloService.hello(..))")
    public void pointCut() {}

    @Before("pointCut()")
    public void before(JoinPoint joinPoint) {
        System.out.println("before ...... " + Arrays.deepToString(joinPoint.getArgs()));
    }

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

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

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

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("around before ......");
        Object proceed = joinPoint.proceed();
        System.out.println("around after ......");
        return proceed;
    }

}

控制器测试

@RestController
public class HelloController {

    @Autowired
    private HelloService helloService;

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

一个切面的执行结果

  1. 当name不为空时
around before ......
before ...... [zhangsan]
HelloServiceImpl hello() ==> zhangsan
around after ......
after ......
afterReturning ......
  1. 当name为空时
around before ......
before ...... [null]
after ......
afterThrowing ......

在这里插入图片描述

多切面的代码

@Component
@Aspect
@Order(1)
class MyAspect01 {

    @Pointcut("execution(* com.example.springboot.service.HelloService.hello(..))")
    public void pointCut() {}

    @Before("pointCut()")
    public void before(JoinPoint joinPoint) {
        System.out.println("MyAspect01 before ...... " + Arrays.deepToString(joinPoint.getArgs()));
    }

    @After("pointCut()")
    public void after() {
        System.out.println("MyAspect01 after ......");
    }

    @AfterReturning("pointCut()")
    public void afterReturning() {
        System.out.println("MyAspect01 afterReturning ......");
    }

    @AfterThrowing("pointCut()")
    public void afterThrowing() {
        System.out.println("MyAspect01 afterThrowing ......");
    }

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("MyAspect01 around before ......");
        Object proceed = joinPoint.proceed();
        System.out.println("MyAspect01 around after ......");
        return proceed;
    }
}


@Component
@Aspect
@Order(2)
class MyAspect02 {

    @Pointcut("execution(* com.example.springboot.service.HelloService.hello(..))")
    public void pointCut() {}

    @Before("pointCut()")
    public void before(JoinPoint joinPoint) {
        System.out.println("MyAspect02 before ...... " + Arrays.deepToString(joinPoint.getArgs()));
    }

    @After("pointCut()")
    public void after() {
        System.out.println("MyAspect02 after ......");
    }

    @AfterReturning("pointCut()")
    public void afterReturning() {
        System.out.println("MyAspect02 afterReturning ......");
    }

    @AfterThrowing("pointCut()")
    public void afterThrowing() {
        System.out.println("MyAspect02 afterThrowing ......");
    }

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("MyAspect02 around before ......");
        Object proceed = joinPoint.proceed();
        System.out.println("MyAspect02 around after ......");
        return proceed;
    }
}

多切面的执行结果

MyAspect01 around before ......
MyAspect01 before ...... [zhangsan]
MyAspect02 around before ......
MyAspect02 before ...... [zhangsan]
HelloServiceImpl hello() ==> zhangsan
MyAspect02 around after ......
MyAspect02 after ......
MyAspect02 afterReturning ......
MyAspect01 around after ......
MyAspect01 after ......
MyAspect01 afterReturning ......

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值