亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring基于XML配置AOP詳解

 更新時(shí)間:2023年09月27日 09:20:02   作者:艾江南  
這篇文章主要介紹了Spring基于XML配置AOP詳解,Spring 的 AOP 功能是基于 AspectJ 實(shí)現(xiàn)的,支持使用 XML 方式定義 AOP 切面,Spring 項(xiàng)目使用 AOP 功能需要定義三個(gè)部分:切面、切點(diǎn)和通知,需要的朋友可以參考下

一、概述

Spring 項(xiàng)目使用 AOP 功能需要定義三個(gè)部分:切面、切點(diǎn)和通知。

二、AOP 使用

Spring 基于 XML 配置 AOP 的方式不會(huì)侵入源碼,但需要維護(hù)更多的配置文件。

1. 定義切面

引用 Spring 管理的 Bean,使用 <aop:aspect> 來(lái)定義切面。

<beans>
    <bean id="demoAspect" class="...DemoAspect"/>
    <aop:config>
        <aop:aspect ref="demoAspect">
            ......
        </aop:aspect>
    </aop:config>
</beans>

2. 定義切點(diǎn)

在切面內(nèi)使用 <aop:pointcut> 來(lái)定義切點(diǎn),然后在通知中使用 pointcut-ref 來(lái)指定切點(diǎn)。

切點(diǎn)表達(dá)式用來(lái)匹配切入的目標(biāo)類和方法。目標(biāo)類只能是 Spring 容器管理的類,切面只能切入 Bean 中的方法。

<beans>
    <bean id="demoAspect" class="...DemoAspect"/>
    <aop:config>
        <aop:aspect ref="demoAspect">
            <aop:pointcut id="myPointcut" expression="execution(* cn.codeartist.spring.aop.xml.*.*(..))"/>
            <aop:before pointcut-ref="myPointcut" method="doBefore"/>
        </aop:aspect>
    </aop:config>
</beans>

切點(diǎn)表達(dá)式也可以在定義通知的時(shí)候指定,而不需要使用 <aop:pointcut> 標(biāo)簽。

<beans>
    <bean id="demoAspect" class="...DemoAspect"/>
    <aop:config>
        <aop:aspect ref="demoAspect">
            <aop:before pointcut="execution(* cn.codeartist.spring.aop.xml.*.*(..))" method="doBefore"/>
        </aop:aspect>
    </aop:config>
</beans>

3. 定義通知

定義通知的時(shí)候需要指定切點(diǎn),通知的類型決定了切入的節(jié)點(diǎn)。

圖片

在切面里使用通知標(biāo)簽中的 method 屬性來(lái)綁定方法。

public class DemoAspect {
    public void doBefore(JoinPoint joinPoint) {
        // do something
    }
    public void doAfter(JoinPoint joinPoint) {
        // do something
    }
    public void doAfterReturning(JoinPoint joinPoint) {
        // do something
    }
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        // do something
        Object proceed = joinPoint.proceed();
        // do something
        return proceed;
    }
    public void doAfterThrowing(JoinPoint joinPoint) {
        // do something
    }
}

前置通知

使用 <aop:before> 定義前置通知,在方法執(zhí)行前添加操作。

<aop:config>
    <aop:aspect ref="demoAspect">
        <aop:pointcut id="myPointcut" expression="execution(* cn.codeartist.spring.aop.xml.*.*(..))"/>
        <aop:before pointcut-ref="myPointcut" method="doBefore"/>
    </aop:aspect>
</aop:config>

后置通知

使用 <aop:after-returning> 注解定義后置通知,在方法正常返回時(shí)執(zhí)行,方法拋異常不執(zhí)行。

<aop:config>
    <aop:aspect ref="demoAspect">
        <aop:pointcut id="myPointcut" expression="execution(* cn.codeartist.spring.aop.xml.*.*(..))"/>
        <aop:after-returning pointcut-ref="myPointcut" method="doAfterReturning"/>
    </aop:aspect>
</aop:config>

環(huán)繞通知

使用 <aop:around> 注解定義環(huán)繞通知,切入方法前后,相當(dāng)于攔截器的功能,可以捕獲異常處理。

環(huán)繞通知的切入點(diǎn)參數(shù)為 ProceedingJoinPoint,并且需要手動(dòng)調(diào)用 proceed() 來(lái)執(zhí)行切入點(diǎn)方法的邏輯。

<aop:config>
    <aop:aspect ref="demoAspect">
        <aop:pointcut id="myPointcut" expression="execution(* cn.codeartist.spring.aop.xml.*.*(..))"/>
        <aop:around pointcut-ref="myPointcut" method="doAround"/>
    </aop:aspect>
</aop:config>

最終通知

使用 <aop:after> 注解定義最終通知,在方法退出時(shí)執(zhí)行,無(wú)論是正常退出還是異常退出。

<aop:config>
    <aop:aspect ref="demoAspect">
        <aop:pointcut id="myPointcut" expression="execution(* cn.codeartist.spring.aop.xml.*.*(..))"/>
        <aop:after pointcut-ref="myPointcut" method="doAfter"/>
    </aop:aspect>
</aop:config>

異常通知

使用 <aop:after-throwing> 注解定義異常通知,在方法拋出異常時(shí)執(zhí)行。

<aop:config>
    <aop:aspect ref="demoAspect">
        <aop:pointcut id="myPointcut" expression="execution(* cn.codeartist.spring.aop.xml.*.*(..))"/>
        <aop:after-throwing pointcut-ref="myPointcut" method="doAfterThrowing"/>
    </aop:aspect>
</aop:config>

4. 通過(guò) Advisor 實(shí)現(xiàn)

使用 Advisor 能以編程的方式創(chuàng)建切面,需要實(shí)現(xiàn)通知的 API 來(lái)定義通知的類型。

比起使用注解定義切點(diǎn),這種方式指定切點(diǎn)表達(dá)式更靈活。

<beans>
    <bean id="beforeAdvice" class="...BeforeAdvice"/>
    <aop:config>
        <aop:advisor pointcut="execution(* cn.codeartist.spring.aop.xml.*.*(..))" advice-ref="beforeAdvice"/>
    </aop:config>
</beans>

到此這篇關(guān)于Spring基于XML配置AOP詳解的文章就介紹到這了,更多相關(guān)Spring配置AOP內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

  • Java生成MD5加密字符串代碼實(shí)例

    Java生成MD5加密字符串代碼實(shí)例

    這篇文章主要介紹了Java生成MD5加密字符串代碼實(shí)例,本文對(duì)MD5的作用作了一些介紹,然后給出了Java下生成MD5加密字符串的代碼示例,需要的朋友可以參考下
    2015-06-06
  • Java Springboot如何基于圖片生成下載鏈接

    Java Springboot如何基于圖片生成下載鏈接

    這篇文章主要介紹了Java Springboot如何基于圖片生成下載鏈接,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • Java實(shí)現(xiàn)簡(jiǎn)單GUI登錄和注冊(cè)界面

    Java實(shí)現(xiàn)簡(jiǎn)單GUI登錄和注冊(cè)界面

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單GUI登錄和注冊(cè)界面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Java實(shí)現(xiàn)HDFS文件上傳下載

    Java實(shí)現(xiàn)HDFS文件上傳下載

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)HDFS文件上傳下載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Java線程并發(fā)訪問(wèn)代碼分析

    Java線程并發(fā)訪問(wèn)代碼分析

    這篇文章主要介紹了Java線程并發(fā)訪問(wèn)代碼分析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Mybatis-Plus select不列出全部字段的方法

    Mybatis-Plus select不列出全部字段的方法

    這篇文章主要介紹了Mybatis-Plus select不列出全部字段的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • java實(shí)現(xiàn)五子棋大戰(zhàn)

    java實(shí)現(xiàn)五子棋大戰(zhàn)

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)五子棋大戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 詳解Java設(shè)計(jì)模式編程中命令模式的項(xiàng)目結(jié)構(gòu)實(shí)現(xiàn)

    詳解Java設(shè)計(jì)模式編程中命令模式的項(xiàng)目結(jié)構(gòu)實(shí)現(xiàn)

    這篇文章主要介紹了Java設(shè)計(jì)模式編程中命令模式的項(xiàng)目結(jié)構(gòu)實(shí)現(xiàn),命令模式將請(qǐng)求與執(zhí)行分離,可以多個(gè)命令接口的實(shí)現(xiàn)類,隱藏真實(shí)的被調(diào)用方,需要的朋友可以參考下
    2016-04-04
  • 最新評(píng)論