SpringBoot AOP处理请求

1.引入相关依赖

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

2.创建TimeAspect类

@Aspect
@Component
public class TimeAspect {

    private final static Logger logger = LoggerFactory.getLogger(TimeAspect.class);

    @Pointcut("execution(public * com.springboot.springbootlearning.controller.TestController.*(..))")
    public void log(){}

    @Before("log()")
    public void doBefore(JoinPoint joinPoint){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        //url
        logger.info("url={}",request.getRequestURI());
        //method
        logger.info("method={}",request.getMethod());
        //ip
        logger.info("ip={}",request.getRemoteAddr());
        //类方法
        logger.info("class_method={}",joinPoint.getSignature().getDeclaringType()+"."+joinPoint.getSignature().getName());
        //参数
        logger.info("args={}",joinPoint.getArgs());
    }

    @After("log()")
    public void doAfter(){
        logger.info("doAfter");
    }

    @AfterReturning(returning = "object",pointcut = "log()")
    public void doAfterReturning(Object object)
    {
        logger.info("response={}",object.toString());
    }
}

 

使用环绕通知实现

package com.lzc.aop.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
public class HelloAspect {
    @Around("execution(* com.lzc.aop.controller.*.*(..))") // 这里是指controller包下所有类的所有方法
    public Object helloAspect(ProceedingJoinPoint jp) {
        try {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            System.out.println("拦截地址:" + request.getRequestURI());
            Object result = jp.proceed(); // 获取请求返回的结果
            return result;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("拦截出现异常:");
        }
        return null;
    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot AOP是Spring框架的一个核心功能之一,它可以用于全局请求日志处理等方面。在Spring Boot中使用AOP,首先需要创建一个Spring Boot Web项目,并引入相关的依赖。这些依赖包括org.springframework.boot:spring-boot-starter、org.springframework.boot:spring-boot-starter-aop和org.springframework.boot:spring-boot-starter-web。 AOP的源码在Spring Boot中位于spring-boot-autoconfigure-2.3.7.RELEASE.jar这个jar包中,该jar包是Spring Boot的自动配置jar包之一。通过引入这些依赖,我们可以使用Spring Boot AOP来实现一些切面编程的功能,比如全局请求日志处理等。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [SpringBootAOP的使用](https://blog.csdn.net/weixin_45583303/article/details/118565966)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [SpringBoot之Spring核心AOP详解](https://blog.csdn.net/qq_42263280/article/details/127696254)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值