博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot | 实现切面
阅读量:4182 次
发布时间:2019-05-26

本文共 2542 字,大约阅读时间需要 8 分钟。

一、引入AOP依赖

org.springframework.boot
spring-boot-starter-aop

 

二、具体编码实现

package com.xiaobu.aspect;import lombok.extern.slf4j.Slf4j;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.*;import org.springframework.stereotype.Component;import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;import java.util.Arrays;/** * @author xiaobu * @version JDK1.8.0_171 * @date on  2019/1/29 11:46 * @description V1.0  切面实现 */@Slf4j@Component@Aspectpublic class WebAspect {    @Resource    private HttpServletRequest request;    @Pointcut("execution(* com.xiaobu.controller.back.*.*(..))")    public void webLog() {    }    /**     * 前置通知     */    @Before("webLog()")    public void doBefore(JoinPoint joinPoint) throws Throwable {        System.out.println("前置通知 = " + joinPoint);    }    /**     * 声明环绕通知     */    @Around("webLog()")    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {        System.out.println("进入方法---环绕通知");        // 记录下请求内容        log.info("URL : " + request.getRequestURL().toString());        log.info("HTTP_METHOD : " + request.getMethod());        log.info("IP : " + request.getRemoteAddr());        log.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());        log.info("ARGS : " + Arrays.toString(joinPoint.getArgs()));        return joinPoint.proceed();    }    /**     * 声明例外通知     */    @AfterThrowing(pointcut = "webLog()", throwing = "e")    public void doAfterThrowing(Exception e) {        System.out.println("例外通知");        System.out.println(e.getMessage());    }    /**     * 声明最终通知     */    @After("webLog()")    public void doAfter() {        System.out.println("最终通知");    }    /**     * 声明后置通知     */    @AfterReturning(pointcut = "webLog()", returning = "result")    public void doAfterReturning(String result) {        System.out.println("后置通知");        System.out.println("---" + result + "---");    }}

 

三、测试

package com.xiaobu.controller.back;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * @author xiaobu * @version JDK1.8.0_171 * @date on  2019/1/31 9:36 * @description V1.0 */@RestController@RequestMapping("back")public class BackController {    @GetMapping("demo")    public String demo(){        return "succees";    }}

四、结果

转载地址:http://nfgai.baihongyu.com/

你可能感兴趣的文章
android studio SDK开发
查看>>
studio 统计代码的行数
查看>>
字符数组和16进制互换
查看>>
PHP项目中出现致命错误: Class 'Redis' not found
查看>>
There is no tracking information for the current branch.
查看>>
fatal: refusing to merge unrelated histories
查看>>
Git命令还原未提交的变更
查看>>
Linux系统中环境变量的配置
查看>>
Linux系统中配置脚本程序开机启动
查看>>
让Linux系统上的nginx支持php程序
查看>>
源码编译安装LNMP环境之Nginx篇
查看>>
源码编译安装LNMP环境之PHP篇
查看>>
Linux中rpm工具使用教程
查看>>
Linux中yum工具使用教程
查看>>
C++字符串函数
查看>>
mknod详解
查看>>
linux中的run-level何解?
查看>>
Linux内核编译详解(转自linuxSir)
查看>>
实模式,保护模式与V86模式
查看>>
628. Maximum Product of Three Numbers(排序)
查看>>