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

Java_Spring之XML?的?AOP?配置

 更新時(shí)間:2023年04月06日 10:40:16   作者:JiangTao_xlili  
這篇文章主要介紹了Java_Spring中基于XML的AOP配置,上篇講到的是基于注解的AOP配置,對(duì)XML感興趣的同學(xué)可以參考閱讀本文

1 環(huán)境搭建

  • 示例:
    • 在學(xué)習(xí) spring 的 aop 時(shí),采用賬戶轉(zhuǎn)賬作為示例。把 spring 的 ioc 也一起應(yīng)用進(jìn)來(lái)。

1.1 第一步:準(zhǔn)備必要的代碼

  • 此處包含了實(shí)體類,業(yè)務(wù)層和持久層代碼。沿用上一章節(jié)中的代碼即可。

1.2 第二步:拷貝必備的 jar 包到工程的 lib 目錄

  • 此處要拷貝 spring 的 ioc 和 aop 兩組 jar 包

1.3 第三步:創(chuàng)建 spring 的配置文件并導(dǎo)入約束

  • 此處要導(dǎo)入 aop 的約束
<?xml version="1.0" encoding="UTF-8"?><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"></beans&gt;

1.4 第四步:配置 spring 的 ioc

<!-- 配置 service --><bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"> <property name="accountDao" ref="accountDao"></property></bean><!-- 配置 dao --><bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"> <property name="dbAssit" ref="dbAssit"></property></bean><!-- 配置數(shù)據(jù)庫(kù)操作對(duì)象 --><bean id="dbAssit" class="com.itheima.dbassit.DBAssit"> <property name="dataSource" ref="dataSource"></property> <!-- 指定 connection 和線程綁定 --> <property name="useCurrentConnection" value="true"></property></bean><!-- 配置數(shù)據(jù)源 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///spring_day02"></property> <property name="user" value="root"></property> <property name="password" value="1234"></property></bean>

1.5 第五步:抽取公共代碼制作成通知

  • 事務(wù)控制類
public class TransactionManager {
 
    //定義一個(gè) DBAssit
    private DBAssit dbAssit ;
    
    public void setDbAssit(DBAssit dbAssit) {
        this.dbAssit = dbAssit;
    }
 
    //開啟事務(wù)
    public void beginTransaction() {
 
    
        try {
        
            dbAssit.getCurrentConnection().setAutoCommit(false);
        } catch (SQLException e) {
 
            e.printStackTrace();
        }
    }
 
 
    //提交事務(wù)
    public void commit() {
 
        try {
            dbAssit.getCurrentConnection().commit();
        } catch (SQLException e) {
 
            e.printStackTrace();
        }
    }
 
    //回滾事務(wù)
    public void rollback() {
 
        try {
            dbAssit.getCurrentConnection().rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
 
    //釋放資源
    public void release() {
 
        try {
            dbAssit.releaseConnection();
        } catch (Exception e) {
 
            e.printStackTrace();
        }
    }
}

2 配置步驟

2.1 第一步:把通知類用 bean 標(biāo)簽配置起來(lái)

<!-- 配置通知 --><bean id="txManager" class="com.itheima.utils.TransactionManager"> <property name="dbAssit" ref="dbAssit"></property></bean> 

2.2 第二步:使用 aop:config 聲明 aop 配置

  • aop:config:
    • 作用:用于聲明開始 aop 的配置
<aop:config> <!-- 配置的代碼都寫在此處 --></aop:config>

2.3 第三步:使用 aop:aspect 配置切面

  • aop:aspect:
    • 作用:
      • 用于配置切面。
    • 屬性:
      • id:給切面提供一個(gè)唯一標(biāo)識(shí)。
      • ref:引用配置好的通知類 bean 的 id。
<aop:aspect id="txAdvice" ref="txManager"> <!--配置通知的類型要寫在此處--></aop:aspect>

2.4 第四步:使用 aop:pointcut 配置切入點(diǎn)表達(dá)式

  • aop:pointcut:
    • 作用:
      • 用于配置切入點(diǎn)表達(dá)式。就是指定對(duì)哪些類的哪些方法進(jìn)行增強(qiáng)。
    • 屬性:
      • expression:用于定義切入點(diǎn)表達(dá)式。
      • id:用于給切入點(diǎn)表達(dá)式提供一個(gè)唯一標(biāo)識(shí)
<aop:pointcut expression="execution( public void com.itheima.service.impl.AccountServiceImpl.transfer( java.lang.String, java.lang.String, java.lang.Float ))" id="pt1"/>

2.5 第五步:使用 aop:xxx 配置對(duì)應(yīng)的通知類型

  • aop:before
    • 作用:
      • 用于配置前置通知。指定增強(qiáng)的方法在切入點(diǎn)方法之前執(zhí)行
    • 屬性:
      • method:用于指定通知類中的增強(qiáng)方法名稱
      • ponitcut-ref:用于指定切入點(diǎn)的表達(dá)式的引用
      • poinitcut:用于指定切入點(diǎn)表達(dá)式
    • 執(zhí)行時(shí)間點(diǎn):
      • 切入點(diǎn)方法執(zhí)行之前執(zhí)行
<aop:before method="beginTransaction" pointcut-ref="pt1"/>
  • aop:after-returning
    • 作用:
      • 用于配置后置通知
    • 屬性:
      • method:指定通知中方法的名稱。
      • pointct:定義切入點(diǎn)表達(dá)式
      • pointcut-ref:指定切入點(diǎn)表達(dá)式的引用
    • 執(zhí)行時(shí)間點(diǎn):
      • 切入點(diǎn)方法正常執(zhí)行之后。它和異常通知只能有一個(gè)執(zhí)行
<aop:after-returning method="commit" pointcut-ref="pt1"/>
  • aop:after-throwing
    • 作用:
      • 用于配置異常通知
    • 屬性:
      • method:指定通知中方法的名稱。
      • pointct:定義切入點(diǎn)表達(dá)式
      • pointcut-ref:指定切入點(diǎn)表達(dá)式的引用
    • 執(zhí)行時(shí)間點(diǎn):
      • 切入點(diǎn)方法執(zhí)行產(chǎn)生異常后執(zhí)行。它和后置通知只能執(zhí)行一個(gè)
<aop:after-throwing method="rollback" pointcut-ref="pt1"/>
  • aop:after
    • 作用:
      • 用于配置最終通知
    • 屬性:
      • method:指定通知中方法的名稱。
      • pointct:定義切入點(diǎn)表達(dá)式
      • pointcut-ref:指定切入點(diǎn)表達(dá)式的引用
    • 執(zhí)行時(shí)間點(diǎn):
      • 無(wú)論切入點(diǎn)方法執(zhí)行時(shí)是否有異常,它都會(huì)在其后面執(zhí)行。
<aop:after method="release" pointcut-ref="pt1"/>

3 切入點(diǎn)表達(dá)式說(shuō)明

  • execution:匹配方法的執(zhí)行(常用)
    • execution(表達(dá)式)
  • 表達(dá)式語(yǔ)法:execution([修飾符] 返回值類型 包名.類名.方法名(參數(shù)))
  • 寫法說(shuō)明:
    • 全匹配方式:
public void com.itheima.service.impl.AccountServiceImpl.saveAccount(com.itheima.domain.Account)

訪問(wèn)修飾符可以省略

void com.itheima.service.impl.AccountServiceImpl.saveAccount(com.itheima.domain.Account)

返回值可以使用*號(hào),表示任意返回值

* com.itheima.service.impl.AccountServiceImpl.saveAccount(com.itheima.domain.Account)

包名可以使用*號(hào),表示任意包,但是有幾級(jí)包,需要寫幾個(gè)*

* *.*.*.*.AccountServiceImpl.saveAccount(com.itheima.domain.Account)

使用..來(lái)表示當(dāng)前包,及其子包

* com..AccountServiceImpl.saveAccount(com.itheima.domain.Account)

類名可以使用*號(hào),表示任意類

* com..*.saveAccount(com.itheima.domain.Account)

方法名可以使用*號(hào),表示任意方法

* com..*.*( com.itheima.domain.Account)

參數(shù)列表可以使用*,表示參數(shù)可以是任意數(shù)據(jù)類型,但是必須有參數(shù)

* com..*.*(*)

參數(shù)列表可以使用..表示有無(wú)參數(shù)均可,有參數(shù)可以是任意類型

* com..*.*(..)

?????全通配方式:

* *..*.*(..)

注: 通常情況下,我們都是對(duì)業(yè)務(wù)層的方法進(jìn)行增強(qiáng),所以切入點(diǎn)表達(dá)式都是切到業(yè)務(wù)層實(shí)現(xiàn)類。

execution(* com.itheima.service.impl.*.*(..))

4 環(huán)繞通知

配置方式:

<aop:config>
    <aop:pointcut expression="execution(* com.itheima.service.impl.*.*(..))" id="pt1"/>
    <aop:aspect id="txAdvice" ref="txManager">
        <!-- 配置環(huán)繞通知 -->
        <aop:around method="transactionAround" pointcut-ref="pt1"/>
    </aop:aspect>
</aop:config>
  • aop:around:
    • 作用:
      • 用于配置環(huán)繞通知
    • 屬性:
      • method:指定通知中方法的名稱。
      • pointct:定義切入點(diǎn)表達(dá)式
      • pointcut-ref:指定切入點(diǎn)表達(dá)式的引用
    • 說(shuō)明:
      • 它是 spring 框架為我們提供的一種可以在代碼中手動(dòng)控制增強(qiáng)代碼什么時(shí)候執(zhí)行的方式。
    • 注意:
      • 通常情況下,環(huán)繞通知都是獨(dú)立使用的
      • /**
      • * 環(huán)繞通知
      • * @param pjp
      • * spring 框架為我們提供了一個(gè)接口:ProceedingJoinPoint,它可以作為環(huán)繞通知的方法參數(shù)。
      • * 在環(huán)繞通知執(zhí)行時(shí),spring 框架會(huì)為我們提供該接口的實(shí)現(xiàn)類對(duì)象,我們直接使用就行。
      • * @return
      • */
public Object transactionAround(ProceedingJoinPoint pjp) {
 
    //定義返回值
    Object rtValue = null;
 
    try {
 
        //獲取方法執(zhí)行所需的參數(shù)
        Object[] args = pjp.getArgs();
 
        //前置通知:開啟事務(wù)
        beginTransaction();
 
        //執(zhí)行方法
        rtValue = pjp.proceed(args);
 
        //后置通知:提交事務(wù)
        commit();
    }catch(Throwable e) {
 
        //異常通知:回滾事務(wù)
        rollback();
        e.printStackTrace();
    }finally {
 
        //最終通知:釋放資源
        release();
    }
 
    return rtValue;
}

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

相關(guān)文章

最新評(píng)論