Spring Boot學(xué)習(xí)入門之AOP處理請(qǐng)求詳解
前言
面向切面(AOP)Aspect Oriented Programming是一種編程范式,與語(yǔ)言無(wú)關(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之后,就可以通過(guò)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í)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Java KindEditor粘貼圖片自動(dòng)上傳到服務(wù)器功能實(shí)現(xiàn)
這篇文章主要介紹了Java KindEditor粘貼圖片自動(dòng)上傳到服務(wù)器功能實(shí)現(xiàn),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04
IDEA錯(cuò)誤:找不到或無(wú)法加載主類的完美解決方法
使用IDEA開(kāi)始就一直在搭建java環(huán)境,許久沒(méi)有使用過(guò)java,剛開(kāi)始有些生疏,先建了一個(gè)最簡(jiǎn)單的類可是運(yùn)行的時(shí)候出現(xiàn)錯(cuò)誤:找不到或無(wú)法加載主類,下面這篇文章主要給大家介紹了關(guān)于IDEA錯(cuò)誤:找不到或無(wú)法加載主類的完美解決方法,需要的朋友可以參考下2022-07-07
Java static方法用法實(shí)戰(zhàn)案例總結(jié)
這篇文章主要介紹了Java static方法用法,結(jié)合具體案例形式總結(jié)分析了java static方法功能、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-09-09
Spring使用RestTemplate和Junit單元測(cè)試的注意事項(xiàng)
這篇文章主要介紹了Spring使用RestTemplate和Junit單元測(cè)試的注意事項(xiàng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
多線程下嵌套異步任務(wù)導(dǎo)致程序假死問(wèn)題
這篇文章主要介紹了多線程下嵌套異步任務(wù)導(dǎo)致程序假死問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
簡(jiǎn)單總結(jié)Java的反射機(jī)制的運(yùn)用
這篇文章主要介紹了Java的反射機(jī)制的運(yùn)用,對(duì)一些常用的類作了一些說(shuō)明,需要的朋友可以參考下2015-11-11
BMIDE環(huán)境導(dǎo)入項(xiàng)目報(bào)編碼錯(cuò)誤解決方案
這篇文章主要介紹了BMIDE環(huán)境導(dǎo)入項(xiàng)目報(bào)編碼錯(cuò)誤解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10

