欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring AspectJ 實(shí)現(xiàn)AOP的方法你了解嗎

 更新時(shí)間:2022年01月20日 11:11:46   作者:YSOcean  
這篇文章主要為大家介紹了Spring AspectJ 實(shí)現(xiàn)AOP的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助

1、什么是 AspectJ?

AspectJ是一個(gè)面向切面的框架,它擴(kuò)展了Java語(yǔ)言。AspectJ定義了AOP語(yǔ)法,也可以說(shuō) AspectJ 是一個(gè)基于 Java 語(yǔ)言的 AOP 框架。通常我們?cè)谑褂?Spring AOP 的時(shí)候,都會(huì)導(dǎo)入 AspectJ 的相關(guān) jar 包。

在 spring2.0以后,spring新增了對(duì)AspectJ 切點(diǎn)表達(dá)式的支持;Aspect1.5新增注解功能,通過(guò) JDK5的注解技術(shù),能直接在類(lèi)中定義切面;新版本的 spring 框架,也都建議使用 AspectJ 來(lái)實(shí)現(xiàn) AOP。所以說(shuō)在 spring AOP 的核心包 Spring-aop-3.2.jar 里面也有對(duì) AspectJ 的支持。

2、切入點(diǎn)表達(dá)式

上一篇博客中,我們?cè)趕pring配置文件中配置如下:

<!-- 切入點(diǎn)表達(dá)式 -->
<aop:pointcut expression="execution(* com.ys.aop.*.*(..))" id="myPointCut"/>

那么它表達(dá)的意思是 返回值任意,包名為 com.ys.aop 下的任意類(lèi)名中的任意方法名,參數(shù)任意。那么這到底是什么意思呢?

首先 execution 是 AspectJ 框架定義的一個(gè)切入點(diǎn)函數(shù),其語(yǔ)法形式如下:

execution(modifiers-pattern? ref-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)
             類(lèi)修飾符           返回值           方法所在的包                  方法名                     方法拋出的異常

簡(jiǎn)單點(diǎn)來(lái)說(shuō)就是:

語(yǔ)法:execution(修飾符  返回值  包.類(lèi).方法名(參數(shù)) throws異常)

具體解釋我們用下面一張思維導(dǎo)圖來(lái)看:

注意:如果切入點(diǎn)表達(dá)式有多個(gè)不同目錄呢? 可以通過(guò) || 來(lái)表示或的關(guān)系。  

<aop:pointcut expression="execution(* com.ys.*Service1.*(..)) ||
                          execution(* com.ys.*Service2.*(..))" id="myPointCut"/>

表示匹配 com.ys包下的,以 Service1結(jié)尾或者以Service2結(jié)尾的類(lèi)的任意方法。

AOP 切入點(diǎn)表達(dá)式支持多種形式的定義規(guī)則:

1、execution:匹配方法的執(zhí)行(常用)
        execution(public *.*(..))
2.within:匹配包或子包中的方法(了解)
    within(com.ys.aop..*)
3.this:匹配實(shí)現(xiàn)接口的代理對(duì)象中的方法(了解)
    this(com.ys.aop.user.UserDAO)
4.target:匹配實(shí)現(xiàn)接口的目標(biāo)對(duì)象中的方法(了解)
    target(com.ys.aop.user.UserDAO)
5.args:匹配參數(shù)格式符合標(biāo)準(zhǔn)的方法(了解)
    args(int,int)
6.bean(id)  對(duì)指定的bean所有的方法(了解)
    bean('userServiceId')

2、Aspect 通知類(lèi)型

Aspect 通知類(lèi)型,定義了類(lèi)型名稱(chēng)以及方法格式。類(lèi)型如下:

    before:前置通知(應(yīng)用:各種校驗(yàn))
    在方法執(zhí)行前執(zhí)行,如果通知拋出異常,阻止方法運(yùn)行
afterReturning:后置通知(應(yīng)用:常規(guī)數(shù)據(jù)處理)
    方法正常返回后執(zhí)行,如果方法中拋出異常,通知無(wú)法執(zhí)行
    必須在方法執(zhí)行后才執(zhí)行,所以可以獲得方法的返回值。
around:環(huán)繞通知(應(yīng)用:十分強(qiáng)大,可以做任何事情)
    方法執(zhí)行前后分別執(zhí)行,可以阻止方法的執(zhí)行
    必須手動(dòng)執(zhí)行目標(biāo)方法
afterThrowing:拋出異常通知(應(yīng)用:包裝異常信息)
    方法拋出異常后執(zhí)行,如果方法沒(méi)有拋出異常,無(wú)法執(zhí)行
after:最終通知(應(yīng)用:清理現(xiàn)場(chǎng))
    方法執(zhí)行完畢后執(zhí)行,無(wú)論方法中是否出現(xiàn)異常

這里最重要的是around,環(huán)繞通知,它可以代替上面的任意通知。

在程序中表示的意思如下:

try{
     //前置:before
    //手動(dòng)執(zhí)行目標(biāo)方法
    //后置:afterRetruning
} catch(){
    //拋出異常 afterThrowing
} finally{
    //最終 after
}

對(duì)應(yīng)的 jar 包如下:

我們可以查看源碼:  

3、AOP具體實(shí)例

①、創(chuàng)建接口

package com.ys.aop;
 
public interface UserService {
    //添加 user
    public void addUser();
    //刪除 user
    public void deleteUser();
}

②、創(chuàng)建實(shí)現(xiàn)類(lèi)

package com.ys.aop;
 
public class UserServiceImpl implements UserService{
    @Override
    public void addUser() {
        System.out.println("增加 User");
    }
    @Override
    public void deleteUser() {
        System.out.println("刪除 User");
    }
}

③、創(chuàng)建切面類(lèi)(包含各種通知)  

package com.ys.aop;
 
import org.aspectj.lang.JoinPoint;
 
 
public class MyAspect {
    /**
     * JoinPoint 能獲取目標(biāo)方法的一些基本信息
     * @param joinPoint
     */
    public void myBefore(JoinPoint joinPoint){
        System.out.println("前置通知 : " + joinPoint.getSignature().getName());
    }
     
    public void myAfterReturning(JoinPoint joinPoint,Object ret){
        System.out.println("后置通知 : " + joinPoint.getSignature().getName() + " , -->" + ret);
    }
     
    public void myAfter(){
        System.out.println("最終通知");
    }
 
}

④、創(chuàng)建spring配置文件applicationContext.xml

我們首先測(cè)試前置通知、后置通知、最終通知

<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">
    <!--1、 創(chuàng)建目標(biāo)類(lèi) -->
    <bean id="userService" class="com.ys.aop.UserServiceImpl"></bean>  
    <!--2、創(chuàng)建切面類(lèi)(通知)  -->
    <bean id="myAspect" class="com.ys.aop.MyAspect"></bean>
     
    <!--3、aop編程 
        3.1 導(dǎo)入命名空間
        3.2 使用 <aop:config>進(jìn)行配置
                proxy-target-class="true" 聲明時(shí)使用cglib代理
                如果不聲明,Spring 會(huì)自動(dòng)選擇cglib代理還是JDK動(dòng)態(tài)代理
            <aop:pointcut> 切入點(diǎn) ,從目標(biāo)對(duì)象獲得具體方法
            <aop:advisor> 特殊的切面,只有一個(gè)通知 和 一個(gè)切入點(diǎn)
                advice-ref 通知引用
                pointcut-ref 切入點(diǎn)引用
        3.3 切入點(diǎn)表達(dá)式
            execution(* com.ys.aop.*.*(..))
            選擇方法         返回值任意   包             類(lèi)名任意   方法名任意   參數(shù)任意
     
    -->
    <aop:config>
        <aop:aspect ref="myAspect">
        <!-- 切入點(diǎn)表達(dá)式 -->
        <aop:pointcut expression="execution(* com.ys.aop.*.*(..))" id="myPointCut"/>
        <!-- 3.1 前置通知
                <aop:before method="" pointcut="" pointcut-ref=""/>
                    method : 通知,及方法名
                    pointcut :切入點(diǎn)表達(dá)式,此表達(dá)式只能當(dāng)前通知使用。
                    pointcut-ref : 切入點(diǎn)引用,可以與其他通知共享切入點(diǎn)。
                通知方法格式:public void myBefore(JoinPoint joinPoint){
                    參數(shù)1:org.aspectj.lang.JoinPoint  用于描述連接點(diǎn)(目標(biāo)方法),獲得目標(biāo)方法名等
        -->
        <aop:before method="myBefore" pointcut-ref="myPointCut"/>
         
         
        <!-- 3.2后置通知  ,目標(biāo)方法后執(zhí)行,獲得返回值
                <aop:after-returning method="" pointcut-ref="" returning=""/>
                    returning 通知方法第二個(gè)參數(shù)的名稱(chēng)
                通知方法格式:public void myAfterReturning(JoinPoint joinPoint,Object ret){
                    參數(shù)1:連接點(diǎn)描述
                    參數(shù)2:類(lèi)型Object,參數(shù)名 returning="ret" 配置的
        -->
        <aop:after-returning method="myAfterReturning" pointcut-ref="myPointCut" returning="ret" />
             
        <!-- 3.3 最終通知 -->        
        <aop:after method="myAfter" pointcut-ref="myPointCut"/>  
             
        </aop:aspect>
    </aop:config>
</beans>

⑤、測(cè)試  

@Test
    public void testAop(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService useService = (UserService) context.getBean("userService");
        useService.addUser();
    }

控制臺(tái)打?。?/strong>

注意,后置通知的返回值為 null,是因?yàn)槲覀兊哪繕?biāo)方法 addUser() 沒(méi)有返回值。如果有返回值,這里就是addUser() 的返回值。

4、測(cè)試異常通知

目標(biāo)接口保持不變,目標(biāo)類(lèi)我們手動(dòng)引入異常:

public void addUser() {
        int i = 1/0;//顯然這里會(huì)拋出除數(shù)不能為 0
        System.out.println("增加 User");
    }

接著配置切面:MyAspect.java

public void myAfterThrowing(JoinPoint joinPoint,Throwable e){
        System.out.println("拋出異常通知 : " + e.getMessage());
    }public void myAfterThrowing(JoinPoint joinPoint,Throwable e){
        System.out.println("拋出異常通知 : " + e.getMessage());
    }

接著在 applicationContext.xml 中配置如下:

<!-- 3.4 拋出異常
                <aop:after-throwing method="" pointcut-ref="" throwing=""/>
                    throwing :通知方法的第二個(gè)參數(shù)名稱(chēng)
                通知方法格式:public void myAfterThrowing(JoinPoint joinPoint,Throwable e){
                    參數(shù)1:連接點(diǎn)描述對(duì)象
                    參數(shù)2:獲得異常信息,類(lèi)型Throwable ,參數(shù)名由throwing="e" 配置
        -->
        <aop:after-throwing method="myAfterThrowing" pointcut-ref="myPointCut" throwing="e"/>

測(cè)試:

@Test
    public void testAop(){
        String str = "com/ys/execption/applicationContext.xml";
        ApplicationContext context = new ClassPathXmlApplicationContext(str);
        UserService useService = (UserService) context.getBean("userService");
        useService.addUser();
    }

控制臺(tái)打印:  

5、測(cè)試環(huán)繞通知

目標(biāo)接口和目標(biāo)類(lèi)保持不變,切面MyAspect 修改如下:

public class MyAspect {
     
    public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable{
        System.out.println("前置通知");
        //手動(dòng)執(zhí)行目標(biāo)方法
        Object obj = joinPoint.proceed();
         
        System.out.println("后置通知");
        return obj;
    }
 
}

applicationContext.xml 配置如下:

<!-- 環(huán)繞通知
                <aop:around method="" pointcut-ref=""/>
                通知方法格式:public Object myAround(ProceedingJoinPoint joinPoint) throws Throwable{
                    返回值類(lèi)型:Object
                    方法名:任意
                    參數(shù):org.aspectj.lang.ProceedingJoinPoint
                    拋出異常
                執(zhí)行目標(biāo)方法:Object obj = joinPoint.proceed();
        -->
        <aop:around method="myAround" pointcut-ref="myPointCut"/>

測(cè)試:

@Test
    public void testAop(){
        String str = "com/ys/around/applicationContext.xml";
        ApplicationContext context = new ClassPathXmlApplicationContext(str);
        UserService useService = (UserService) context.getBean("userService");
        useService.addUser();
    }

印結(jié)果:

  

那么至此,通過(guò) xml 配置的方式我們講解了Spring AOP 的配置。下一章將通過(guò)注解的方式來(lái)實(shí)現(xiàn)。

總結(jié)

本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

  • java發(fā)送form-data請(qǐng)求實(shí)現(xiàn)文件上傳的示例代碼

    java發(fā)送form-data請(qǐng)求實(shí)現(xiàn)文件上傳的示例代碼

    最近做一個(gè)需求,需要請(qǐng)求第三方接口上傳文件,該請(qǐng)求類(lèi)型是form-data請(qǐng)求,本文就來(lái)介紹一下java發(fā)送form-data請(qǐng)求實(shí)現(xiàn)文件上傳的示例代碼,感興趣的可以了解一下
    2023-12-12
  • 一文帶你搞懂Maven的繼承與聚合

    一文帶你搞懂Maven的繼承與聚合

    這篇文章主要為大家詳細(xì)介紹了Maven的繼承和聚合以及二者的區(qū)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-07-07
  • Spring Boot兩種配置文件properties和yml區(qū)別

    Spring Boot兩種配置文件properties和yml區(qū)別

    這篇文章主要為大家介紹了java面試中常見(jiàn)問(wèn)到的Spring Boot兩種配置文件properties和yml區(qū)別解答,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • Java實(shí)現(xiàn)冒泡排序算法

    Java實(shí)現(xiàn)冒泡排序算法

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)冒泡排序算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • kafka生產(chǎn)實(shí)踐(詳解)

    kafka生產(chǎn)實(shí)踐(詳解)

    下面小編就為大家?guī)?lái)一篇kafka生產(chǎn)實(shí)踐(詳解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-08-08
  • 阿里、華為、騰訊Java技術(shù)面試題精選

    阿里、華為、騰訊Java技術(shù)面試題精選

    這篇文章主要為大家分享了阿里、華為、騰訊Java技術(shù)面試題精選,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • spring boot 不連接數(shù)據(jù)庫(kù)啟動(dòng)的解決

    spring boot 不連接數(shù)據(jù)庫(kù)啟動(dòng)的解決

    這篇文章主要介紹了spring boot 不連接數(shù)據(jù)庫(kù)啟動(dòng)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java 多線(xiàn)程死鎖的產(chǎn)生以及如何避免死鎖

    Java 多線(xiàn)程死鎖的產(chǎn)生以及如何避免死鎖

    這篇文章主要介紹了Java 多線(xiàn)程死鎖的產(chǎn)生以及如何避免死鎖,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • SpringBoot中定義Bean的方法總結(jié)

    SpringBoot中定義Bean的方法總結(jié)

    在Spring Boot應(yīng)用程序中,定義Bean是非常常見(jiàn)的操作,它是構(gòu)建應(yīng)用程序的基礎(chǔ),pring Boot提供了多種方式來(lái)定義Bean,每種方式都有其適用的場(chǎng)景和優(yōu)勢(shì),本文將介紹Spring Boot中定義Bean的幾種常見(jiàn)方式,需要的朋友可以參考下
    2023-12-12
  • Java 類(lèi)動(dòng)態(tài)添加屬性字段的操作

    Java 類(lèi)動(dòng)態(tài)添加屬性字段的操作

    這篇文章主要介紹了Java 類(lèi)動(dòng)態(tài)添加屬性字段的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-02-02

最新評(píng)論