Spring AOP的注解快速应用

一、新建一个类,AOP用来增强该类的方法

package com.example.demo.aop;

import org.springframework.stereotype.Component;

@Component
public class AopService {

    public void testAop(int id) {
        System.out.println("【Test Aop id = " + id + "】");
    }

}

二、创建AOP的注解,进行切面编程

package com.example.demo.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class MyAopAspect {


    // 【配置切点 及要传的参数】
    @Pointcut("execution(* com.example.demo.aop.AopService.testAop(..)) && args(id)")
    public void pointCut(int id) {

    }


    // 前置通知
    @Before("pointCut(id)")
    public void before(int id) {

        System.out.println("【前置通知】id=" + id);
    }


    // 环绕通知
    @Around("pointCut(id)")
    public Object aroundLog(ProceedingJoinPoint joinpoint, int id) {
        Object res = null;
        try {
            System.out.println("【环绕通知】Begin, id=" + id);

            long start = System.currentTimeMillis();
            // 有返回参数 则需返回值
            res = joinpoint.proceed();
            long end = System.currentTimeMillis();
            System.out.println("【方法耗时】" + (end - start) + " ms");

            System.out.println("【环绕通知】End, id=" + id);
        } catch (Throwable e) {
            System.out.println("【环绕通知异常】" + e.getMessage());
        }
        return res;
    }


    // 后置通知
    @After("pointCut(id)")
    public void after(int id) {

        System.out.println("【后置通知】id=" + id);
    }


    // 方法执行完成
    @AfterReturning("pointCut(id)")
    public void afterReturning(int id) {

        System.out.println("【方法执行完成】id=" + id);
    }


    // 方法异常
    @AfterThrowing("pointCut(id)")
    public void afterThrowingLog(int id) {

        System.out.println("【方法异常】id=" + id);
    }


}

–> pointCut的配置规则说明

三、测试

package com.example.demo;

import com.example.demo.aop.AopService;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;


@RunWith(SpringRunner.class)
@SpringBootTest
class MyTest {

    @Autowired
    private AopService aopService;



    @Test
    public void testAop() {
        //AOP使用注解
        aopService.testAop(100);
    }


}

四、相关依赖包

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
 </dependency>
 
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
</dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值