SpringBoot學(xué)習(xí)入門之AOP處理請(qǐng)求詳解

前言

在青岡等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站 網(wǎng)站設(shè)計(jì)制作按需網(wǎng)站制作,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站設(shè)計(jì),網(wǎng)絡(luò)營(yíng)銷推廣,外貿(mào)營(yíng)銷網(wǎng)站建設(shè),青岡網(wǎng)站建設(shè)費(fèi)用合理。

面向切面(AOP)Aspect Oriented Programming是一種編程范式,與語(yǔ)言無關(guān),是一種程序設(shè)計(jì)思想,它也是spring的兩大核心之一。

在spring Boot中,如何用AOP實(shí)現(xiàn)攔截器呢?

首先加入依賴關(guān)系:

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

希望截?cái)r如下Controller:

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

首先要?jiǎng)?chuàng)建一個(gè)攔截類:RequestInterceptor

并且使用@Aspect和@Component標(biāo)注這個(gè)類:

@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等注解就非常輕松的實(shí)現(xiàn)截?cái)r功能。

這里需要處理請(qǐng)求,所以我們需要在攔截器中獲取請(qǐng)求。

只需要在方法體中使用:

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é)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)創(chuàng)新互聯(lián)的支持。

網(wǎng)站名稱:SpringBoot學(xué)習(xí)入門之AOP處理請(qǐng)求詳解
路徑分享:http://muchs.cn/article24/jpgjje.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、企業(yè)建站關(guān)鍵詞優(yōu)化ChatGPT、網(wǎng)站維護(hù)、移動(dòng)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司