springboot使用AOP

1、添加依赖

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

2、设置切面

@Aspect
@Configuration
public class AopConfig {

    @Pointcut("execution(* com.glodon.springboot.controller.AopController.test*(..))")
    public void excudeService(){}

    @Before("excudeService()")
    public void before() {
        System.out.println("切面before执行了");
    }

    @After("excudeService()")
    public void after() {
        System.out.println("切面after执行了");
    }

    @AfterReturning("excudeService()")
    public void afterReturning() {
        System.out.println("切面afterReturning执行了");
    }

    @AfterThrowing("excudeService()")
    public void afterThrowing() {
        System.out.println("切面afterThrowing执行了");
    }

    @Around("excudeService()")
    public Object around(ProceedingJoinPoint thisJoinPoint){
        Object obj = null;
        System.err.println ("切面around before执行了");
        try {
            thisJoinPoint.proceed();
        } catch (Throwable e) {
            e.printStackTrace ();
        }
        System.err.println ("切面around after执行了");
        return obj;
    }
    
}

 3、测试类

@RestController
@RequestMapping(value = "/api/aop")
public class AopController {

    @GetMapping(value = "/test")
    public String testAOP() {
        System.out.println("test");
        return "test";
    }

}

 4、AOP顺序

通过注解方式配置AOP是无序的,若想有序则需显式定义一个Advisor的实现了Ordered的子类的bean,并且给order赋值


更多:

SpringBoot简介

SpringBoot-HelloWorld

spring boot 框架解析

spring boot 部署、启动

spring boot配置文件

spring boot 全局异常捕捉

springboot使用fastjson

springboot使用定时任务、异步

springboot使用AOP

springboot使用拦截器

springboot输出日志

springboot集成hibernate-jpa方式


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值