Java Spring AOP詳解
1.什么是AOP?
AOP為Aspect Oriented Programming的縮寫,意為:面向切面編程,通過預(yù)編譯方式和運(yùn)行期間動態(tài)代理實(shí)現(xiàn)程序功能的統(tǒng)一維護(hù)的一種技術(shù)。AOP是OOP的延續(xù),是軟件開發(fā)中的一個(gè)熱點(diǎn),也是Spring框架中的一個(gè)重要內(nèi)容,是函數(shù)式編程的一種衍生范型。利用AOP可以對業(yè)務(wù)邏輯的各個(gè)部分進(jìn)行隔離,從而使得業(yè)務(wù)邏輯各部分之間的耦合度降低,提高程序的可重用性,同時(shí)提高了開發(fā)的效率。
2.AOP在Spring中的作用
提供聲明式事務(wù):允許用戶自定義切面
- 橫切關(guān)注點(diǎn):跨越應(yīng)用程序多個(gè)模塊的方法或功能.既是,與我們業(yè)務(wù)邏輯無關(guān),但是我們需要關(guān)注的部分,就是橫切關(guān)注點(diǎn).如日志,安全,緩存,事務(wù)等…
- 切面(ASPECT):橫切關(guān)注點(diǎn) 被模塊化 的特殊對象。即,它是一個(gè)類。
- 通知(Advice):切面必須要完成的工作。即,它是類中的一個(gè)方法。
- 目標(biāo)(Target):被通知對象。
- 代理(Proxy):向目標(biāo)對象應(yīng)用通知之后創(chuàng)建的對象。
- 切入點(diǎn)(PointCut):切面通知 執(zhí)行的 “地點(diǎn)”的定義。
- 連接點(diǎn)(JointPoint):與切入點(diǎn)匹配的執(zhí)行點(diǎn)。
SpringAOP中,通過Advice定義橫切邏輯,Spring中支持5種類型的Advice:
即 Aop 在 不改變原有代碼的情況下 , 去增加新的功能 .
3.使用Spring實(shí)現(xiàn)AOP
【重點(diǎn)】需要導(dǎo)入一個(gè)依賴包!
<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.4</version> </dependency>
execution (* com.db.dao….(…)) 整個(gè)表達(dá)式可以分為五個(gè)部分:
1、execution(): 表達(dá)式主體。
2、第一個(gè)*星號:表示返回類型,星號表示所有的類型。注意星號后面有個(gè)空格。
3、包名com.db.dao:表示需要攔截的包名,只攔截這個(gè)包,其他包不攔截。
報(bào)名后面的兩個(gè)句點(diǎn)表示當(dāng)前包和當(dāng)前包的所有子包,com.db.dao包、子孫包下所有類的方法。4、第二個(gè)*號:表示類名,星號表示所有的類。
5、*(…)最后這個(gè)星號表示方法名,星號表示所有的方法,后面括弧里面表示方法的參數(shù),兩個(gè)句點(diǎn)表示任何參數(shù)。
方式一:使用Spring的接口
通過 Spring API 實(shí)現(xiàn)
首先編寫我們的業(yè)務(wù)接口和實(shí)現(xiàn)類
public interface UserService { public void add(); public void delete(); public void update(); public void query(); }
public class UserServiceImpl implements UserService{ @Override public void add() { System.out.println("add"); } @Override public void delete() { System.out.println("delete"); } @Override public void update() { System.out.println("update"); } @Override public void query() { System.out.println("query"); } }
然后去寫我們的增強(qiáng)類 , 我們編寫兩個(gè) , 一個(gè)前置增強(qiáng) 一個(gè)后置增強(qiáng)
public class BeforeLog implements MethodBeforeAdvice { /* method:要執(zhí)行的目標(biāo)對象的方法 objects:參數(shù) target:目標(biāo)對象 */ @Override public void before(Method method, Object[] objects, Object target) throws Throwable { System.out.println(target.getClass().getName()+"的"+method.getName()); } }
public class AfterLog implements AfterReturningAdvice { @Override //returnValue:返回值 public void afterReturning(Object returnValue, Method method, Object[] objects, Object o1) throws Throwable { System.out.println("執(zhí)行了"+method.getName()+"方法,返回結(jié)果為:"+returnValue); } }
最后去spring的文件中注冊 , 并實(shí)現(xiàn)aop切入實(shí)現(xiàn) , 注意導(dǎo)入約束 .
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--注冊bean--> <bean id="userService" class="com.kuang.service.UserServiceImpl"/> <bean id="log" class="com.kuang.log.Log"/> <bean id="afterLog" class="com.kuang.log.AfterLog"/> <!--aop的配置--> <aop:config> <!--切入點(diǎn) expression:表達(dá)式匹配要執(zhí)行的方法--> <aop:pointcut id="pointcut" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/> <!--執(zhí)行環(huán)繞; advice-ref執(zhí)行方法 . pointcut-ref切入點(diǎn)--> <aop:advisor advice-ref="log" pointcut-ref="pointcut"/> <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/> </aop:config> </beans>
測試
public class MyTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //動態(tài)代理的必須是接口 UserService userService = (UserService) context.getBean("userService"); userService.add(); userService.delete(); userService.update(); userService.query(); } }
方法二:使用自定義類來實(shí)現(xiàn)
目標(biāo)業(yè)務(wù)類不變依舊是userServiceImpl
第一步 : 寫我們自己的一個(gè)切入類
//自定義切入點(diǎn)類 public class DiyPoint { public void before(){ System.out.println("-----方法執(zhí)行前-----"); } public void after(){ System.out.println("-----方法執(zhí)行后-----"); } }
在Spring中配置
<!--第二種方式自定義實(shí)現(xiàn)--> <!--注冊bean--> <bean id="diy" class="com.kuang.config.DiyPointcut"/> <!--aop的配置--> <aop:config> <!--第二種方式:使用AOP的標(biāo)簽實(shí)現(xiàn)--> <aop:aspect ref="diy"> <aop:pointcut id="diyPonitcut" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/> <aop:before pointcut-ref="diyPonitcut" method="before"/> <aop:after pointcut-ref="diyPonitcut" method="after"/> </aop:aspect> </aop:config>
方法三:使用注解實(shí)現(xiàn)
第一步:編寫一個(gè)注解實(shí)現(xiàn)的增強(qiáng)類
@Aspect //標(biāo)注這個(gè)類為一個(gè)切面 public class AnnotationPointCut { @Before("execution(* com.hui.service.UserServiceImpl.*(..))") public void before(){ System.out.println("-----方法執(zhí)行前-----"); } @After("execution(* com.hui.service.UserServiceImpl.*(..))") public void after(){ System.out.println("-----方法執(zhí)行后-----"); } //在環(huán)繞增強(qiáng)中,我們可以給定一個(gè)參數(shù),代表我們要獲取處理切入的點(diǎn) @Around("execution(* com.hui.service.UserServiceImpl.*(..))") public void around(ProceedingJoinPoint jp) throws Throwable { System.out.println("環(huán)繞前"); //執(zhí)行方法 Object proceed = jp.proceed(); System.out.println("環(huán)繞后"); } }
第二步:在Spring配置文件中,注冊bean,并增加支持注解的配置
<!-- 方式三--> <bean id="annotationPointCut" class="com.hui.diy.AnnotationPointCut"/> <!--開啟注解支持--> <aop:aspectj-autoproxy/>
aop:aspectj-autoproxy說明:
通過aop命名空間的<aop:aspectj-autoproxy />聲明自動為spring容器中那些配置@aspectJ切面的bean創(chuàng)建代理,織入切面。當(dāng)然,spring 在內(nèi)部依舊采用AnnotationAwareAspectJAutoProxyCreator進(jìn)行自動代理的創(chuàng)建工作,但具體實(shí)現(xiàn)的細(xì)節(jié)已經(jīng)被<aop:aspectj-autoproxy />隱藏起來了
<aop:aspectj-autoproxy
/>有一個(gè)proxy-target-class屬性,默認(rèn)為false,表示使用jdk動態(tài)代理織入增強(qiáng),當(dāng)配為<aop:aspectj-autoproxy
poxy-target-class=“true”/>時(shí),表示使用CGLib動態(tài)代理技術(shù)織入增強(qiáng)。不過即使proxy-target-class設(shè)置為false,如果目標(biāo)類沒有聲明接口,則spring將自動使用CGLib動態(tài)代理。
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
spring?boot?validation參數(shù)校驗(yàn)與分組嵌套各種類型及使用小結(jié)
參數(shù)校驗(yàn)基本上是controller必做的事情,畢竟前端傳過來的一切都不可信,validation可以簡化這一操作,這篇文章主要介紹了spring?boot?validation參數(shù)校驗(yàn)分組嵌套各種類型及使用小結(jié),需要的朋友可以參考下2023-09-09Java基礎(chǔ)知識之CharArrayWriter流的使用
這篇文章主要介紹了Java基礎(chǔ)知識之CharArrayWriter流的使用,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12淺談Java循環(huán)中的For和For-each哪個(gè)更快
本文主要介紹了淺談Java循環(huán)中的For和For-each哪個(gè)更快,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08Spring boot2X Consul如何使用Feign實(shí)現(xiàn)服務(wù)調(diào)用
這篇文章主要介紹了spring boot2X Consul如何使用Feign實(shí)現(xiàn)服務(wù)調(diào)用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12SpringBoot整合Elasticsearch實(shí)現(xiàn)索引和文檔的操作方法
Elasticsearch 基于 Apache Lucene 構(gòu)建,采用 Java 編寫,并使用 Lucene 構(gòu)建索引、提供搜索功能,本文分步驟通過綜合案例給大家分享SpringBoot整合Elasticsearch的相關(guān)知識,感興趣的朋友跟隨小編一起看看吧2021-05-05