首页 > Java > java教程 > 正文

springboot中aop的使用介绍(代码示例)

不言
发布: 2018-10-15 15:51:15
转载
2441人浏览过

本篇文章给大家带来的内容是关于springboot中aop的使用介绍(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

第一步:添加依赖

        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-aop</artifactId>  
        </dependency>
登录后复制

第二步:定义一个切面类

package com.example.demo.aop;
import java.lang.reflect.Method;
import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import static com.sun.xml.internal.ws.dump.LoggingDumpTube.Position.Before;
@Component
@Aspect // 将一个java类定义为切面类
@Order(-1)//如果有多个aop,这里可以定义优先级,越小级别越高
public class LogDemo {
    private static final Logger LOG = LoggerFactory.getLogger(LogDemo.class);

    @Pointcut("execution(* com.example.demo.test.TestController.test(..))")//两个..代表所有子目录,最后括号里的两个..代表所有参数
    public void logPointCut() {
    }
    @Before("logPointCut()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        // 接收到请求,记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        System.out.println("before");

    }

    @After(value = "logPointCut()")
    public void after(JoinPoint joinPoint) {
        System.out.println("after");
    }


    @AfterReturning(returning = "ret", pointcut = "logPointCut()")// returning的值和doAfterReturning的参数名一致
    public void doAfterReturning(Object ret) throws Throwable {
        System.out.println("AfterReturning");
    }

    @Around("logPointCut()")
    public void doAround(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("around1");
        Object ob = pjp.proceed();//环绕通知的进程方法不能省略,否则可能导致无法执行
        System.out.println("around2");
    }
}
登录后复制

注意:

如果同一个 切面类,定义了定义了两个 @Before,那么这两个 @Before的执行顺序是无法确定的

对于@Around,不管它有没有返回值,但是必须要方法内部,调用一下 pjp.proceed();否则,Controller 中的接口将没有机会被执行,从而也导致了 @Before不会被触发

测试的controller如下:

package com.example.demo.test;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class TestController {

    @RequestMapping(value = "test",method = RequestMethod.GET)
    @ResponseBody
    public String test(String name){
        System.out.println("============method");
        return name;
    }
}
登录后复制

配置完成,看看效果,输出如下:

around1
before============method
around2
after
AfterReturning
登录后复制

可以看到,切面方法的执行如下:

around-->before-->method-->around-->after-->AfterReturning

如果配置了@AfterThrowing,当有异常时,执行如下:

around-->before-->method-->around-->after-->AfterThrowing

以上就是springboot中aop的使用介绍(代码示例)的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:博客园网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号