Spring Boot学习入门之AOP处理请求详解

作者:a60782885 时间:2023-11-27 10:55:17 

前言

面向切面(AOP)Aspect Oriented Programming是一种编程范式,与语言无关,是一种程序设计思想,它也是spring的两大核心之一。

在spring Boot中,如何用AOP实现 * 呢?

首先加入依赖关系:

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

希望截拦如下Controller:

@RestController
public class MyController {
@RequestMapping(value="/hello", method=RequestMethod.GET)
public String hello() {
 return "";
}
}

首先要创建一个拦截类:RequestInterceptor

并且使用@Aspect和@Component标注这个类:

@Component
@Aspect
public class RequestInterceptor {
@Pointcut("execution(* com.example.controller.*.*(..))")
public void pointcut1() {}
@Before("pointcut1()")
public void doBefore() {
 System.out.println("before");
}
@Around("pointcut1()")
public void around(ProceedingJoinPoint thisJoinPoint) throws Throwable {
 System.out.println("around1");
 thisJoinPoint.proceed();
 System.out.println("around2");
}
@After("pointcut1()")
public void after(JoinPoint joinPoint) {
 System.out.println("after");
}
@AfterReturning("pointcut1()")
public void afterReturning(JoinPoint joinPoint) {
 System.out.println("afterReturning");
}
@AfterThrowing("pointcut1()")
public void afterThrowing(JoinPoint joinPoint) {
 System.out.println("afterThrowing");
}
}

只需要使用@Before,@After等注解就非常轻松的实现截拦功能。

这里需要处理请求,所以我们需要在 * 中获取请求。

只需要在方法体中使用:

ServletRequestAttributes attributes =(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();

就可以获取到request。

同理也可以在After等方法中获取response。

获取request之后,就可以通过request获取url,ip等信息。

如果我们想要获取当前正在拦截的方法的信息。可以使用JoinPoint。

例如:

@After("pointcut1()")
public void after(JoinPoint joinPoint) {
logger.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName()+ "." + joinPoint.getSignature().getName());
System.out.println("after");
}

就可以获取包名,类名,方法名。

来源:http://blog.csdn.net/a60782885/article/details/68489520

标签:springboot,aop,处理请求
0
投稿

猜你喜欢

  • spring boot 图片上传与显示功能实例详解

    2021-07-02 09:46:02
  • Android4.2中全屏或者取消标题栏的方法总结

    2023-06-14 16:17:06
  • C#实现冒泡排序和插入排序算法

    2021-07-18 17:01:53
  • spring中的注解事务演示和添加步骤详情

    2023-03-03 08:32:48
  • 解决response.setHeader设置下载文件名无效的问题

    2021-08-15 20:43:54
  • vs 中C#项目读取JSON配置文件的方法

    2022-09-22 15:04:05
  • Android设置默认锁屏壁纸接口的方法

    2021-09-25 00:16:42
  • C++之try catch 异常处理入门实例

    2021-09-13 04:42:07
  • spring cloud gateway中如何读取请求参数

    2021-08-19 16:19:02
  • C++智能指针读书笔记

    2022-02-24 06:51:28
  • android自定义控件实现简易时间轴(1)

    2022-09-24 00:42:23
  • Android Glide的简单使用

    2022-12-22 01:37:14
  • Java内存区域与内存溢出异常详解

    2022-09-10 17:01:19
  • Unity3D实现渐变颜色效果

    2022-09-03 08:14:32
  • c#连接mdf文件示例分享

    2022-12-15 10:48:33
  • C# 9使用foreach扩展的示例详解

    2023-01-27 08:35:56
  • java ThreadGroup的作用及方法详解

    2022-02-03 16:49:01
  • 谈一谈Android内存泄漏问题

    2023-01-29 23:23:54
  • Android设计模式系列之工厂方法模式

    2023-08-16 19:10:17
  • Android如何通过组合的方式自定义View

    2022-11-01 04:33:46
  • asp之家 软件编程 m.aspxhome.com