日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Spring-AOP @AspectJ进阶之切点复合运算

發(fā)布時間:2025/3/21 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring-AOP @AspectJ进阶之切点复合运算 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

  • 概述
  • 示例

概述

@AspectJ可以使用切點函數(shù)定義切點,還可以使用邏輯運算符對切點進行復合運算得到復合切點。

為了在切面中重用切點,還可以對切點進行命名,以便在其他地方引用定義過的切點。

當一個連接點匹配多個切點時,需要考慮織入順序的問題,另外一個重要的問題是如何在增強中訪問連接點上下文的信息。


示例

代碼已托管到Github—> https://github.com/yangshangwei/SpringMaster


這里僅僅展示關(guān)鍵代碼

切面部分:

package com.xgj.aop.spring.advisor.aspectJAdvance.pointcutComplex;import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before;/*** * * @ClassName: PointcutComplexAspect* * @Description: 標注了@Aspect的切面* * @author: Mr.Yang* * @date: 2017年9月9日 上午1:50:45*/@Aspect public class PointcutComplexAspect {/*** * * @Title: matchGreetTo* * @Description: 與預算,匹配com.xgj.aop.spring.advisor.aspectJAdvance.* pointcutComplex包中所有g(shù)reetTo()方法的切點* * * @return: void*/@Before("within(com.xgj.aop.spring.advisor.aspectJAdvance.pointcutComplex.*)"+ " && execution(* greetTo(..))")public void matchGreetTo() {System.out.println("matchGreetTo executed,some logic is here ");}/*** * * @Title: something* * @Description: 非與預算,匹配所有的serverTo方法,且不位于WaiterOne目標類切點* * * @return: void*/@After("!target(com.xgj.aop.spring.advisor.aspectJAdvance.pointcutComplex.WaiterOne)"+ " && execution(* serverTo(..))")public void something() {System.out.println("something executed,some logic is here ");}/*** * * @Title: method* * @Description: 或運算,匹配IWaiter和ISeller接口實現(xiàn)類所有連接點的切點* * * @return: void*/@AfterReturning("target(com.xgj.aop.spring.advisor.aspectJAdvance.pointcutComplex.IWaiter)"+ " || target(com.xgj.aop.spring.advisor.aspectJAdvance.pointcutComplex.ISeller)")public void method() {System.out.println("method executed,some logic is here ");}}

配置文件,Bean的初始化使用context:component-scan掃描

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- (1)聲明Context命名空間以及Schema文件 (2)掃描類包以及應(yīng)用注解定義的bean --> <context:component-scan base-package="com.xgj.aop.spring.advisor.aspectJAdvance.pointcutComplex"/><!-- 基于@AspectJ切面的驅(qū)動器 --> <aop:aspectj-autoproxy proxy-target-class="true"/><!-- 使用了@AspectJ注解的切面類 --> <bean class="com.xgj.aop.spring.advisor.aspectJAdvance.pointcutComplex.PointcutComplexAspect"/></beans>

測試類:

package com.xgj.aop.spring.advisor.aspectJAdvance.pointcutComplex;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class PointcutComplexAspectTest {@Testpublic void test() {ApplicationContext ctx = new ClassPathXmlApplicationContext("com/xgj/aop/spring/advisor/aspectJAdvance/pointcutComplex/conf-pointcutComplex.xml");WaiterOne waiterOne = ctx.getBean("waiterOne", WaiterOne.class);WaiterTwo waiterTwo = ctx.getBean("waiterTwo", WaiterTwo.class);SellerOne sellerOne = ctx.getBean("sellerOne", SellerOne.class);SellerTwo sellerTwo = ctx.getBean("sellerTwo", SellerTwo.class);waiterOne.greetTo("XiaoGongJiang");System.out.println();waiterOne.serverTo("XiaoGongJiang");System.out.println();waiterTwo.greetTo("XiaoGongJiang");System.out.println();sellerOne.greetTo("XiaoGongJiang");System.out.println();sellerOne.greetTo("XiaoGongJiang");System.out.println();} } 《新程序員》:云原生和全面數(shù)字化實踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的Spring-AOP @AspectJ进阶之切点复合运算的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。