Spring Boot學(xué)習(xí)入門之AOP處理請求詳解
前言
面向切面(AOP)Aspect Oriented Programming是一種編程范式,與語言無關(guān),是一種程序設(shè)計思想,它也是spring的兩大核心之一。
在spring Boot中,如何用AOP實現(xiàn)攔截器呢?
首先加入依賴關(guān)系:
<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 "";
}
}
首先要創(chuàng)建一個攔截類:RequestInterceptor
并且使用@Aspect和@Component標(biāo)注這個類:
@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等注解就非常輕松的實現(xiàn)截攔功能。
這里需要處理請求,所以我們需要在攔截器中獲取請求。
只需要在方法體中使用:
ServletRequestAttributes attributes =(ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest();
就可以獲取到request。
同理也可以在After等方法中獲取response。
獲取request之后,就可以通過request獲取url,ip等信息。
如果我們想要獲取當(dāng)前正在攔截的方法的信息??梢允褂肑oinPoint。
例如:
@After("pointcut1()")
public void after(JoinPoint joinPoint) {
logger.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName()+ "." + joinPoint.getSignature().getName());
System.out.println("after");
}
就可以獲取包名,類名,方法名。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
Java幸運28系統(tǒng)搭建數(shù)組的使用實例詳解
在本篇文章里小編給大家整理了關(guān)于Java幸運28系統(tǒng)搭建數(shù)組的使用實例內(nèi)容,有需要的朋友們可以參考學(xué)習(xí)下。2019-09-09
Java中finally關(guān)鍵字對返回值的影響詳解
這篇文章主要介紹了Java中finally關(guān)鍵字對返回值的影響詳解,執(zhí)行完try catch里面內(nèi)容準(zhǔn)備return時,如果還有finally需要執(zhí)行這是編譯器會為我們增加一個全局變量去暫存return 的值,等到finally執(zhí)行完成去return這個全局變量,需要的朋友可以參考下2024-01-01
spring boot+ redis 接口訪問頻率限制的實現(xiàn)
這篇文章主要介紹了spring boot+ redis 接口訪問頻率限制的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
使用Spring?Boot+gRPC構(gòu)建微服務(wù)并部署的案例詳解
這篇文章主要介紹了使用Spring?Boot+gRPC構(gòu)建微服務(wù)并部署,Spring Cloud僅僅是一個開發(fā)框架,沒有實現(xiàn)微服務(wù)所必須的服務(wù)調(diào)度、資源分配等功能,這些需求要借助Kubernetes等平臺來完成,本文給大家介紹的非常詳細,需要的朋友參考下吧2022-06-06
MyBatis-Plus中使用EntityWrappe進行列表數(shù)據(jù)倒序設(shè)置方式
這篇文章主要介紹了MyBatis-Plus中使用EntityWrappe進行列表數(shù)據(jù)倒序設(shè)置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Netty組件NioEventLoopGroup創(chuàng)建線程執(zhí)行器源碼解析
這篇文章主要介紹了Netty組件NioEventLoopGroup創(chuàng)建線程執(zhí)行器源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-03-03

