SpringAop使用-創(chuàng)新互聯(lián)

文章目錄
  • Spring Aop
  • 基本概念
  • 簡單使用
  • AspectJ基本語法
    • 切點(diǎn)表達(dá)式
    • 在函數(shù)中使用通配符
    • 邏輯運(yùn)算符
    • 不同的增強(qiáng)類型
      • 引介方法例子
  • 切點(diǎn)函數(shù)詳解
    • @annotation
    • execution
      • 通過方法簽名定義
      • 通過類來定義
      • 通過類包來定義

創(chuàng)新互聯(lián)公司專注于蔡甸企業(yè)網(wǎng)站建設(shè),自適應(yīng)網(wǎng)站建設(shè),商城網(wǎng)站制作。蔡甸網(wǎng)站建設(shè)公司,為蔡甸等地區(qū)提供建站服務(wù)。全流程按需定制,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)Spring Aop

基于AspectJ和基于schema的Aop命名的使用

在main方法中寫上 System.getProperties().put(“sun.misc.ProxyGenerator.saveGeneratedFiles”, “true”);可以在com.sun.proxy路徑下看到生成的代理類class

基本概念
  • 連接點(diǎn)(Joinpoint) 所有可以執(zhí)行額外代碼的地方,比如方法前,方法后的時(shí)候
  • 切入點(diǎn)(Pointcut): 從眾多的連接點(diǎn)根據(jù)條件找到實(shí)際執(zhí)行代碼的地方的點(diǎn)
  • 增強(qiáng)(Advice): 實(shí)際執(zhí)行額外邏輯的代碼
  • 目標(biāo)對(duì)象(targer) :被增強(qiáng)目標(biāo)類
  • 引介(Introduction):可以讓目標(biāo)類實(shí)現(xiàn)額外得方法,類似動(dòng)態(tài)繼承接口
  • 織入(weaving):將增強(qiáng)加入到切點(diǎn)得這個(gè)過程。有編譯器織入(AspectJ原生),裝載器織入(AspectJ原生),動(dòng)態(tài)代理織入(spring原生aop,spring后面融入AspectJ,但是底層還是動(dòng)態(tài)代理)
  • 代理(proxy):最后實(shí)際運(yùn)行的類,功能和目標(biāo)方法一樣,只是加入了增強(qiáng)
  • 切面(Aspect):切入點(diǎn)+增強(qiáng)
簡單使用

目標(biāo)對(duì)象的接口以及實(shí)現(xiàn)

public interface Hello {void say();
    String tell(String mes);
}

public class HelloImpl implements Hello {@Override
    public void say() {System.out.println("hi");
    }

    @Override
    public String tell(String mes) {System.out.println(mes);
        return mes;
    }
}

實(shí)現(xiàn)一個(gè)切面

@Aspect
public class BeformAspect {@Before("execution(* say(..))")
    public void print()
    {System.out.println("before");
    }
}
  • @Aspect表示定位為一個(gè)切面
    在這里插入圖片描述
    基于schema配置xml

使用

ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
Hello hello = (Hello)applicationContext.getBean("hello");
hello.say();
AspectJ基本語法 切點(diǎn)表達(dá)式

在這里插入圖片描述

在函數(shù)中使用通配符
符號(hào)作用
*匹配任意字符,但只能匹配上下文種的一個(gè)
…(兩個(gè)點(diǎn))匹配任意字符可以匹配上下文多個(gè),但在表示類的時(shí)候,必須和*聯(lián)合使用,表示入?yún)为?dú)使用
+必須跟在類名后,代表類的子類,包括自己

在這里插入圖片描述

邏輯運(yùn)算符

在這里插入圖片描述

不同的增強(qiáng)類型
注解參數(shù)作用
@Before (前置)value定義切點(diǎn)
argNames可以獲得目標(biāo)對(duì)象的參數(shù),參數(shù)名要和目標(biāo)的參數(shù)名一致,多個(gè)已逗號(hào)隔開
@AfterReturning (后置)value定義切點(diǎn)
pointcut顯示定義將覆蓋value,和value同義
returning將目標(biāo)的返回值綁定給增強(qiáng)方法
argNames同前面一樣
@Around (環(huán)繞)value定義切點(diǎn)
argNames同前面一樣
@AfterThrowing (異常)value定義切點(diǎn)
pointcut同前面一樣
throwing將拋出的異常綁定到方法中
argNames同前面一樣
@After (不管異常還是正常退出都會(huì)執(zhí)行)value定義切點(diǎn)
argNames同前面一樣
@DeclareParents (引介增強(qiáng))value定義切點(diǎn)
defaultImpl默認(rèn)的接口實(shí)現(xiàn)類
引介方法例子

比較特殊不同于其他增強(qiáng)類型的使用,
將HelloImpl融入SellerImpl類

public interface Hello {void say();
    String tell(String mes);
}

public class HelloImpl implements Hello {@Override
    public void say() {System.out.println("hi");
    }

    @Override
    public String tell(String mes) {System.out.println(mes);
        return mes;
    }
}

public interface Seller {String sell(String goods);
}

public class SellerImpl implements Seller{@Override
    public String sell(String goods) {System.out.println(goods);
        return null;
    }
}

介引切面

@Aspect
public class BeformAspect {@DeclareParents(value = "AspectJ.HelloImpl",defaultImpl = SellerImpl.class)
    public Seller seller;

}
  • value 切點(diǎn),找到需要介引的目標(biāo)
  • defaultImpl 找到需要增強(qiáng)的具體實(shí)現(xiàn)類
  • 下面的變量為defaultImpl的接口

配置文件

使用

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        //可以將HelloImpl強(qiáng)轉(zhuǎn)為Seller
        Seller hello = (Seller)applicationContext.getBean("hello");
        System.out.println(hello);
切點(diǎn)函數(shù)詳解 @annotation
@Before("@annotation(AspectJ.Mytest)")

被注解標(biāo)記的方法會(huì)執(zhí)行切面注入

execution 通過方法簽名定義

匹配 所有public的方法(其他修飾符不行),第一個(gè)*代表返回值 第二個(gè)代表方法名,(…)表示任意入?yún)?/p>

@Before("execution(public * *(..))")

任意返回值,任意參數(shù),方法名字以ll結(jié)尾的方法

@Before("execution(* *ll(..))")
通過類來定義

匹配Hello接口所有實(shí)現(xiàn)類,第一個(gè)*為返回值。(只包括接口定義的方法)

@Before("execution(* AspectJ.Hello.*(..))")

匹配Hello接口所有實(shí)現(xiàn)類,第一個(gè)*為返回值。(包括子類里不是接口的方法)

@Before("execution(* AspectJ.Hello+.*(..))")
通過類包來定義

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧

本文標(biāo)題:SpringAop使用-創(chuàng)新互聯(lián)
瀏覽路徑:http://muchs.cn/article24/dheoje.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、品牌網(wǎng)站建設(shè)網(wǎng)站內(nèi)鏈、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站收錄小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司