日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當(dāng)前位置:首頁 > 科技  > 軟件

你了解Spring AOP的這個技能點嗎?有什么應(yīng)用場景?

來源: 責(zé)編: 時間:2023-12-08 09:13:55 254觀看
導(dǎo)讀環(huán)境:Spring5.3.231. 介紹今天看Spring文檔看到這么一個知識點《Control Flow Pointcuts》都不好翻譯官方原文:Spring control flow pointcuts are conceptually similar to AspectJ cflow pointcuts, although less po

環(huán)境:Spring5.3.23qft28資訊網(wǎng)——每日最新資訊28at.com

1. 介紹

今天看Spring文檔看到這么一個知識點《Control Flow Pointcuts》都不好翻譯

qft28資訊網(wǎng)——每日最新資訊28at.com

qft28資訊網(wǎng)——每日最新資訊28at.com

官方原文:qft28資訊網(wǎng)——每日最新資訊28at.com

Spring control flow pointcuts are conceptually similar to AspectJ cflow pointcuts, although less powerful. (There is currently no way to specify that a pointcut runs below a join point matched by another pointcut.) A control flow pointcut matches the current call stack. For example, it might fire if the join point was invoked by a method in the com.mycompany.web package or by the SomeCaller class. Control flow pointcuts are specified by using the org.springframework.aop.support.ControlFlowPointcut class.qft28資訊網(wǎng)——每日最新資訊28at.com

大意:Spring控制流切入點在概念上類似于aspectj cflow切入點,盡管功能不那么強大。(目前還沒有辦法指定一個切入點在與另一個切入點匹配的連接點下面運行。)控制流切入點與當(dāng)前調(diào)用堆棧匹配。例如,如果連接點由com.mycompany.web包中的方法或someecaller類調(diào)用,則可能會觸發(fā)該連接點。控制流切入點是通過使用org.springframework.aop.support.ControlFlowPointcut類指定的。qft28資訊網(wǎng)——每日最新資訊28at.com

其實看完這個,可能你還是不懂什么意思,接下來我們來跑一個實例,就能明白撒意思了。qft28資訊網(wǎng)——每日最新資訊28at.com

2. Control Flow實例

準(zhǔn)備幾個方法嵌套調(diào)用的類qft28資訊網(wǎng)——每日最新資訊28at.com

static class PersonDAO {  public void save(String name) {    System.out.println("PersonDAO save method invoke...") ;  }}static class PersonService {  private PersonDAO dao ;  public PersonService(PersonDAO dao) {    this.dao = dao ;  }  public void save(String name) {    System.out.println("PersonService save method inovke...") ;    this.dao.save(name) ;  }}static class PersonManager {  private PersonService ps ;  public void setPs(PersonService ps) {    this.ps = ps ;  }  public void index(String name) {    System.out.println("PersonManager index method invoke...") ;    this.ps.save(name) ;  }}

上面的類及方法調(diào)用非常簡單:PersonManager ---> PersonService ---> PersonDAO。接下來是通過編程的方式創(chuàng)建PersonService代理對象。qft28資訊網(wǎng)——每日最新資訊28at.com

// 實例化上面的類PersonDAO dao = new PersonDAO() ;PersonService target = new PersonService(dao) ;PersonManager pm = new PersonManager() ;Class<?> clazz = PersonManager.class ;String methodName = "index" ;// 定義切入點ControlFlowPointcut pointcut = new ControlFlowPointcut(clazz, methodName) ;// 定義通知MethodInterceptor logInterceptor = invocation -> {  System.out.println("before log...") ;  Object ret = invocation.proceed() ;  System.out.println("after log...") ;  return ret ;} ;// 定義切面DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(pointcut, logInterceptor) ;// 通過ProxyFactory創(chuàng)建代理對象,創(chuàng)建的是PersonService對象的代理ProxyFactory factory = new ProxyFactory(target) ;factory.addAdvisor(advisor) ;// 基于CGLIB生成代理factory.setProxyTargetClass(true) ;PersonService ps = (PersonService) factory.getProxy() ;pm.setPs(ps) ;pm.index("張三") ;

控制臺輸出qft28資訊網(wǎng)——每日最新資訊28at.com

PersonManager index method invoke...before log...PersonService save method inovke...PersonDAO save method invoke...after log...

從輸出的結(jié)果發(fā)現(xiàn),在PersonService#save方法之前之前和之后分別打印了日志信息。原理是什么呢?這里我們需要先看ControlFlowPointcut 切入點是如何工作的。qft28資訊網(wǎng)——每日最新資訊28at.com

ControlFlowPointcut核心方法

這里只列出了幾個重要的方法,在spring中只支持方法級別的攔截。qft28資訊網(wǎng)——每日最新資訊28at.com

public class ControlFlowPointcut implements Pointcut, ClassFilter, MethodMatcher, Serializable {  private final Class<?> clazz;  @Nullable  private final String methodName;  // 對應(yīng)類級別的匹配全部返回true,就是都匹配  @Override  public boolean matches(Class<?> clazz) {    return true;  }  // 方法匹配,直接true  @Override  public boolean matches(Method method, Class<?> targetClass) {    return true;  }  // 這里是關(guān)鍵,只有isRuntime返回了true才有可能調(diào)用下面3個參數(shù)的matches方法  @Override  public boolean isRuntime() {    return true;  }  // 該方法的調(diào)用需要上面2個參數(shù)的matches方法返回true且isRuntime方法也返回true才會調(diào)用這里  @Override  public boolean matches(Method method, Class<?> targetClass, Object... args) {    // 遍歷當(dāng)前線程的執(zhí)行棧情況(也就是當(dāng)前方法的調(diào)用棧情況)    for (StackTraceElement element : new Throwable().getStackTrace()) {      // 這里就開始判斷當(dāng)前執(zhí)行的類是否與給定的類相同 && 當(dāng)前設(shè)置的methodName為空或者當(dāng)前棧執(zhí)行的方法名與給定的方法名相同      if (element.getClassName().equals(this.clazz.getName()) &&          (this.methodName == null || element.getMethodName().equals(this.methodName))) {        // 最終這里只有返回了true,我們上面的通知MethodInterceptor才會被執(zhí)行        return true;      }    }    return false;  }}

有了上面源碼的分析后,我們再來看看上面的示例代碼:qft28資訊網(wǎng)——每日最新資訊28at.com

// 指明要匹配的類Class<?> clazz = PersonManager.class ;// 指明要匹配的方法名String methodName = "index" ;/**  * 將傳入到切入點中;而在該切入點的matches方法中進行了判斷, * 整個執(zhí)行的線程棧中的所有類及方法是否與這里給定的相同, * 只有相同了攔截器才能執(zhí)行 */ControlFlowPointcut pointcut = new ControlFlowPointcut(clazz, methodName) ;

分析到這你應(yīng)該知道這個Control Flow有撒用了吧,總結(jié):qft28資訊網(wǎng)——每日最新資訊28at.com

Control Flow就是用來判斷當(dāng)前執(zhí)行的線程棧中(所有方法的調(diào)用)是否與你給定的類及方法匹配,只有匹配了才能執(zhí)行我們的增強(通知)代碼。qft28資訊網(wǎng)——每日最新資訊28at.com

簡單說:我PersonService想監(jiān)控PersonManager中的index方法是否調(diào)用了我。qft28資訊網(wǎng)——每日最新資訊28at.com

官方有這段說明:qft28資訊網(wǎng)——每日最新資訊28at.com

Dynamic pointcuts are costlier to evaluate than static pointcuts. They take into account method arguments as well as static information. This means that they must be evaluated with every method invocation and that the result cannot be cached, as arguments will vary.qft28資訊網(wǎng)——每日最新資訊28at.com

The main example is the control flow pointcut.qft28資訊網(wǎng)——每日最新資訊28at.com

大意:與靜態(tài)快捷方式相比,動態(tài)快捷方式的評估成本更高。它們會考慮方法參數(shù)和靜態(tài)信息。這意味著每次調(diào)用方法時都必須對其進行評估,而且由于參數(shù)會發(fā)生變化,因此無法緩存評估結(jié)果。控制流快捷方式就是一個主要的例子。qft28資訊網(wǎng)——每日最新資訊28at.com

3. Control Flow性能

同樣來自官方說明:qft28資訊網(wǎng)——每日最新資訊28at.com

Control flow pointcuts are significantly more expensive to evaluate at runtime than even other dynamic pointcuts. In Java 1.4, the cost is about five times that of other dynamic pointcuts.qft28資訊網(wǎng)——每日最新資訊28at.com

大意:與其他動態(tài)切入點相比,控制流切入點在運行時評估的成本要高得多。在Java1.4中,成本大約是其他動態(tài)切入點的五倍。qft28資訊網(wǎng)——每日最新資訊28at.com

qft28資訊網(wǎng)——每日最新資訊28at.com

知道了Control Flow怎么一回事,那它有什么使用場景嗎?有使用過的還望能分享下圖片,歡迎大家留言討論。圖片qft28資訊網(wǎng)——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-39502-0.html你了解Spring AOP的這個技能點嗎?有什么應(yīng)用場景?

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: .NET Core 3.1 升級到 .NET 8,看看都有哪些變化

下一篇: 刷了360多道算法題,我終于頓悟了它的真諦

標(biāo)簽:
  • 熱門焦點
  • 6月iOS設(shè)備好評榜:第一蟬聯(lián)榜首近一年

    作為安兔兔各種榜單里變化最小的那個,2023年6月的iOS好評榜和上個月相比沒有任何排名上的變化,僅僅是部分設(shè)備好評率的下降,長年累月的用戶評價和逐漸退出市場的老款機器讓這
  • 5月iOS設(shè)備好評榜:iPhone 14僅排第43?

    來到新的一月,安兔兔的各個榜單又重新匯總了數(shù)據(jù),像安卓陣營的榜單都有著比較大的變動,不過iOS由于設(shè)備的更新?lián)Q代并沒有那么快,所以相對來說變化并不大,特別是iOS好評榜,老款設(shè)
  • 摸魚心法第一章——和配置文件說拜拜

    為了能摸魚我們團隊做了容器化,但是帶來的問題是服務(wù)配置文件很麻煩,然后大家在群里進行了“親切友好”的溝通圖片圖片圖片圖片對比就對比,簡單對比下獨立配置中心和k8s作為配
  • 一篇聊聊Go錯誤封裝機制

    %w 是用于錯誤包裝(Error Wrapping)的格式化動詞。它是用于 fmt.Errorf 和 fmt.Sprintf 函數(shù)中的一個特殊格式化動詞,用于將一個錯誤(或其他可打印的值)包裝在一個新的錯誤中。使
  • 在線圖片編輯器,支持PSD解析、AI摳圖等

    自從我上次分享一個人開發(fā)仿造稿定設(shè)計的圖片編輯器到現(xiàn)在,不知不覺已過去一年時間了,期間我經(jīng)歷了裁員失業(yè)、面試找工作碰壁,寒冬下一直沒有很好地履行計劃.....這些就放在日
  • 一篇文章帶你了解 CSS 屬性選擇器

    屬性選擇器對帶有指定屬性的 HTML 元素設(shè)置樣式??梢詾閾碛兄付▽傩缘?HTML 元素設(shè)置樣式,而不僅限于 class 和 id 屬性。一、了解屬性選擇器CSS屬性選擇器提供了一種簡單而
  • 電視息屏休眠仍有網(wǎng)絡(luò)上傳 愛奇藝被質(zhì)疑“薅消費者羊毛”

    記者丨寧曉敏 見習(xí)生丨汗青出品丨鰲頭財經(jīng)(theSankei) 前不久,愛奇藝發(fā)布了一份亮眼的一季報,不僅營收和會員營收創(chuàng)造歷史最佳表現(xiàn),其運營利潤也連續(xù)6個月實現(xiàn)增長。自去年年初
  • 共享單車的故事講到哪了?

    來源丨海克財經(jīng)與共享充電寶相差不多,共享單車已很久沒有被國內(nèi)熱點新聞關(guān)照到了。除了一再漲價和用戶直呼用不起了。近日多家媒體再發(fā)報道稱,成都、天津、鄭州等地多個共享單
  • 中國家電海外掘金正當(dāng)時|出海專題

    作者|吳南南編輯|胡展嘉運營|陳佳慧出品|零態(tài)LT(ID:LingTai_LT)2023年,出海市場戰(zhàn)況空前,中國創(chuàng)業(yè)者在海外紛紛摩拳擦掌,以期能夠把中國的商業(yè)模式、創(chuàng)業(yè)理念、戰(zhàn)略打法輸出海外,他們依
Top 主站蜘蛛池模板: 哈巴河县| 桃园市| 清河县| 六枝特区| 辽中县| 定安县| 延津县| 昌邑市| 漠河县| 营山县| 丹东市| 惠安县| 广平县| 莱州市| 册亨县| 建湖县| 南充市| 临西县| 介休市| 泰兴市| 吉安市| 诸城市| 信丰县| 扶余县| 扎鲁特旗| 饶河县| 吉安市| 兴和县| 和平区| 义乌市| 侯马市| 奈曼旗| 弥勒县| 清远市| 安溪县| 佛山市| 郓城县| 彩票| 平舆县| 子长县| 广饶县|