Springboot使用切面功能詳解
Spring切面功能介紹
Spring Boot 是一個(gè)基于Spring框架的項(xiàng)目,它簡(jiǎn)化了基于Spring的應(yīng)用開發(fā)。在Spring框架中,切面(Aspect)是一種特殊的類,它定義了橫切關(guān)注點(diǎn)(cross-cutting concerns),比如日志記錄、事務(wù)管理、權(quán)限檢查等。這些關(guān)注點(diǎn)通常橫跨多個(gè)請(qǐng)求處理路徑,因此被稱為“橫切”關(guān)注點(diǎn)。
AOP在spring中又叫“面向切面編程”,它可以說是對(duì)傳統(tǒng)我們面向?qū)ο缶幊痰囊粋€(gè)補(bǔ)充,從字面上顧名思義就可以知道,它的主要操作對(duì)象就是“切面”,所以我們就可以簡(jiǎn)單的理解它是貫穿于方法之中,在方法執(zhí)行前、執(zhí)行時(shí)、執(zhí)行后、返回值后、異常后要執(zhí)行的操作。 相當(dāng)于是將我們?cè)疽粭l線執(zhí)行的程序在中間切開加入了一些其他操作一樣。
下面這個(gè)圖就展示了切面的樣子。我們定義了日志切面、安全切面等,可以實(shí)現(xiàn)統(tǒng)一的日志管理和安全管理。請(qǐng)求來了之后,先依次通過各個(gè)切面,然后調(diào)用業(yè)務(wù)邏輯。返回的時(shí)候再次通過各個(gè)切面。切面的好處我認(rèn)為在兩個(gè)方面:1) 不侵入現(xiàn)有的業(yè)務(wù)代碼 2)一個(gè)切面的代碼,可以實(shí)現(xiàn)對(duì)多個(gè)業(yè)務(wù)模塊進(jìn)行處理,提升了代碼復(fù)用性。
切面定義和使用流程
在Spring框架中,切面是通過使用AOP(面向切面編程)來實(shí)現(xiàn)的。Spring AOP提供了一個(gè)強(qiáng)大而靈活的機(jī)制來實(shí)現(xiàn)切面。以下是在Spring Boot中使用切面的一些基本概念和步驟:
引入依賴 :
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
定義切面(Aspect) :
創(chuàng)建一個(gè)類,使用@Aspect
注解來聲明它是一個(gè)切面。
切點(diǎn)(Pointcut) :
使用@Pointcut
注解來定義一個(gè)切點(diǎn),它指定了哪些方法將被切面攔截。
通知(Advice) :
定義通知,它指定了在切點(diǎn)處應(yīng)該執(zhí)行什么操作。有不同類型的通知,包括:
@Before
:在目標(biāo)方法執(zhí)行之前執(zhí)行。@After
:在目標(biāo)方法執(zhí)行之后執(zhí)行。@AfterReturning
:在目標(biāo)方法成功返回后執(zhí)行。@AfterThrowing
:在目標(biāo)方法拋出異常后執(zhí)行。@Around
:在目標(biāo)方法執(zhí)行前后都執(zhí)行。
織入(Weaving) :
織入是將切面應(yīng)用到目標(biāo)對(duì)象的過程。Spring AOP通過代理機(jī)制來實(shí)現(xiàn)織入。
啟用AOP:
在Spring Boot應(yīng)用中,你可以通過在啟動(dòng)類上添加@EnableAspectJAutoProxy
注解來啟用AOP。
下面是一個(gè)簡(jiǎn)單的Spring Boot切面示例,實(shí)現(xiàn)了日志的切面功能:
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @Aspect // 聲明這是一個(gè)切面類 @Component // 讓Spring容器接管 public class LoggingAspect { // 定義切點(diǎn),攔截com.example包及其子包下的所有方法 @Pointcut("within(com.example..*)") public void loggable() {} // 定義一個(gè)前置通知 @Before("loggable()") public void beforeAdvice() { System.out.println("Before the method is called"); } ``` // 方法正常執(zhí)行完之后 /** * 在程序正常執(zhí)行完之后如果有返回值,我們可以對(duì)這個(gè)返回值進(jìn)行接收 * returning用來接收方法的返回值 * */ @AfterReturning("loggable()",returning="result") public void LogReturn(JoinPoint joinPoint,Object result) { System.out.println("【" + joinPoint.getSignature().getName() + "】程序方法執(zhí)行完畢了...結(jié)果是:" + result); } }
在這個(gè)例子中,LoggingAspect
類定義了一個(gè)切點(diǎn)loggable
,它攔截com.example
包及其子包下的所有方法。beforeAdvice
方法是一個(gè)前置通知,它會(huì)在匹配的每個(gè)方法執(zhí)行之前被調(diào)用。
JointPoint可以獲得方法的相關(guān)信息,如方法的返回值、方法名、參數(shù)類型等。
要使用這個(gè)切面,你需要在Spring Boot的主類上添加@EnableAspectJAutoProxy
注解:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.EnableAspectJAutoProxy; @SpringBootApplication @EnableAspectJAutoProxy public class MyApp { public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } }
這種方式下,除非你對(duì)項(xiàng)目代碼特別熟悉,才知道哪里定義了切面的功能,對(duì)哪些類起作用。
否則,如果你是接手一個(gè)新的項(xiàng)目,或者一個(gè)開源項(xiàng)目,梳理清楚這些關(guān)系還是比較麻煩的。我覺得切面結(jié)合注解來使用,可能更好一些。
切面結(jié)合注解使用
在Spring框架中,切面(Aspect)可以與注解(Annotation)結(jié)合使用,以提供更靈活的橫切關(guān)注點(diǎn)處理。通過注解,你可以在代碼中直接指定哪些方法需要被特定的切面處理,而不需要在切面中硬編碼方法名或類名。這種方式使得代碼更加清晰,并且易于維護(hù)。
以下是如何結(jié)合注解使用切面的步驟:
1. 定義注解
首先,你需要定義一個(gè)注解,這個(gè)注解將被用來標(biāo)記需要被切面處理的方法。
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.METHOD}) // 這個(gè)注解只能應(yīng)用至方法上 @Retention(RetentionPolicy.RUNTIME) // 運(yùn)行時(shí)還會(huì)保留這個(gè)注解 public @interface Loggable { }
2. 創(chuàng)建切面
然后,創(chuàng)建一個(gè)切面類,并在其中定義一個(gè)切點(diǎn)(Pointcut),這個(gè)切點(diǎn)將匹配所有標(biāo)記了@Loggable
注解的方法。
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; @Aspect @Component public class LoggingAspect { // 定義切點(diǎn),匹配所有標(biāo)記了@Loggable注解的方法 @Pointcut("@annotation(com.example.Loggable)") public void loggable() {} // 定義一個(gè)前置通知 @Before("loggable()") public void beforeAdvice(JoinPoint joinPoint) { // 這里利用了JAVA反射的特性 MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Loggable loggable = signature.getMethod().getAnnotation(Loggable.class); if (loggable != null) { System.out.println("Before method: " + joinPoint.getSignature().getName() + " - " + loggable); } } }
在這個(gè)例子中,loggable
切點(diǎn)匹配所有標(biāo)記了@Loggable
注解的方法。beforeAdvice
方法是一個(gè)前置通知,它會(huì)在每個(gè)標(biāo)記了@Loggable
注解的方法執(zhí)行之前被調(diào)用。
3. 使用注解
最后,在你的業(yè)務(wù)邏輯中,使用@Loggable
注解來標(biāo)記需要被切面處理的方法。
import com.example.Loggable; import org.springframework.stereotype.Service; @Service public class MyService { @Loggable public void someMethod() { System.out.println("Executing someMethod"); } }
在這個(gè)例子中,someMethod
方法被標(biāo)記了@Loggable
注解,因此它會(huì)被LoggingAspect
中的beforeAdvice
方法攔截。
4. 啟用AOP
確保你的Spring Boot應(yīng)用已經(jīng)啟用了AOP,如之前所述,可以在啟動(dòng)類上添加@EnableAspectJAutoProxy
注解。
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.EnableAspectJAutoProxy; @SpringBootApplication @EnableAspectJAutoProxy public class MyApp { public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } }
通過這種方式,你可以靈活地使用注解來控制哪些方法需要被切面處理,而不需要在切面中硬編碼方法名或類名,使得代碼更加模塊化和易于維護(hù)。
以上就是Springboot使用切面功能詳解的詳細(xì)內(nèi)容,更多關(guān)于Springboot切面的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
JAVA數(shù)字千分位和小數(shù)點(diǎn)的現(xiàn)實(shí)代碼(處理金額問題)
這篇文章主要介紹了JAVA數(shù)字千分位和小數(shù)點(diǎn)的現(xiàn)實(shí)代碼(處理金額問題),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10mybatis中association和collection的使用與區(qū)別
在 MyBatis 中,<association>?和?<collection>?是用于配置結(jié)果映射中關(guān)聯(lián)關(guān)系的兩個(gè)元素,本文主要介紹了mybatis中<association>和<collection>的使用與區(qū)別,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01Spring Boot插件spring tool suite安裝及使用詳解
這篇文章主要介紹了Spring Boot插件spring tool suite安裝及使用詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08談?wù)凷pring Boot 數(shù)據(jù)源加載及其多數(shù)據(jù)源簡(jiǎn)單實(shí)現(xiàn)(小結(jié))
這篇文章主要介紹了談?wù)凷pring Boot 數(shù)據(jù)源加載及其多數(shù)據(jù)源簡(jiǎn)單實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-04-04SpringCloud之熔斷器Hystrix的實(shí)現(xiàn)
這篇文章主要介紹了SpringCloud之熔斷器Hystrix的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08springmvc集成shiro登錄權(quán)限示例代碼
本篇文章主要介紹了springmvc集成shiro登錄權(quán)限示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02使用mybatis插件PageHelper實(shí)現(xiàn)分頁效果
這篇文章主要為大家詳細(xì)介紹了使用mybatis插件PageHelper實(shí)現(xiàn)分頁效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01Spring-data-JPA使用時(shí)碰到的問題以及解決方案
這篇文章主要介紹了Spring-data-JPA使用時(shí)碰到的問題以及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12