Java_Spring之XML?的?AOP?配置
1 環(huán)境搭建
- 示例:
- 在學(xué)習(xí) spring 的 aop 時,采用賬戶轉(zhuǎn)賬作為示例。把 spring 的 ioc 也一起應(yīng)用進來。
1.1 第一步:準(zhǔn)備必要的代碼
- 此處包含了實體類,業(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>
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ù)庫操作對象 --><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 { //定義一個 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)簽配置起來
<!-- 配置通知 --><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:給切面提供一個唯一標(biāo)識。
- ref:引用配置好的通知類 bean 的 id。
- 作用:
<aop:aspect id="txAdvice" ref="txManager"> <!--配置通知的類型要寫在此處--></aop:aspect>
2.4 第四步:使用 aop:pointcut 配置切入點表達式
- aop:pointcut:
- 作用:
- 用于配置切入點表達式。就是指定對哪些類的哪些方法進行增強。
- 屬性:
- expression:用于定義切入點表達式。
- id:用于給切入點表達式提供一個唯一標(biāo)識
- 作用:
<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 配置對應(yīng)的通知類型
- aop:before
- 作用:
- 用于配置前置通知。指定增強的方法在切入點方法之前執(zhí)行
- 屬性:
- method:用于指定通知類中的增強方法名稱
- ponitcut-ref:用于指定切入點的表達式的引用
- poinitcut:用于指定切入點表達式
- 執(zhí)行時間點:
- 切入點方法執(zhí)行之前執(zhí)行
- 作用:
<aop:before method="beginTransaction" pointcut-ref="pt1"/>
- aop:after-returning
- 作用:
- 用于配置后置通知
- 屬性:
- method:指定通知中方法的名稱。
- pointct:定義切入點表達式
- pointcut-ref:指定切入點表達式的引用
- 執(zhí)行時間點:
- 切入點方法正常執(zhí)行之后。它和異常通知只能有一個執(zhí)行
- 作用:
<aop:after-returning method="commit" pointcut-ref="pt1"/>
- aop:after-throwing
- 作用:
- 用于配置異常通知
- 屬性:
- method:指定通知中方法的名稱。
- pointct:定義切入點表達式
- pointcut-ref:指定切入點表達式的引用
- 執(zhí)行時間點:
- 切入點方法執(zhí)行產(chǎn)生異常后執(zhí)行。它和后置通知只能執(zhí)行一個
- 作用:
<aop:after-throwing method="rollback" pointcut-ref="pt1"/>
- aop:after
- 作用:
- 用于配置最終通知
- 屬性:
- method:指定通知中方法的名稱。
- pointct:定義切入點表達式
- pointcut-ref:指定切入點表達式的引用
- 執(zhí)行時間點:
- 無論切入點方法執(zhí)行時是否有異常,它都會在其后面執(zhí)行。
- 作用:
<aop:after method="release" pointcut-ref="pt1"/>
3 切入點表達式說明
- execution:匹配方法的執(zhí)行(常用)
- execution(表達式)
- 表達式語法:execution([修飾符] 返回值類型 包名.類名.方法名(參數(shù)))
- 寫法說明:
- 全匹配方式:
public void com.itheima.service.impl.AccountServiceImpl.saveAccount(com.itheima.domain.Account)
訪問修飾符可以省略
void com.itheima.service.impl.AccountServiceImpl.saveAccount(com.itheima.domain.Account)
返回值可以使用*號,表示任意返回值
* com.itheima.service.impl.AccountServiceImpl.saveAccount(com.itheima.domain.Account)
包名可以使用*號,表示任意包,但是有幾級包,需要寫幾個*
* *.*.*.*.AccountServiceImpl.saveAccount(com.itheima.domain.Account)
使用..來表示當(dāng)前包,及其子包
* com..AccountServiceImpl.saveAccount(com.itheima.domain.Account)
類名可以使用*號,表示任意類
* com..*.saveAccount(com.itheima.domain.Account)
方法名可以使用*號,表示任意方法
* com..*.*( com.itheima.domain.Account)
參數(shù)列表可以使用*,表示參數(shù)可以是任意數(shù)據(jù)類型,但是必須有參數(shù)
* com..*.*(*)
參數(shù)列表可以使用..表示有無參數(shù)均可,有參數(shù)可以是任意類型
* com..*.*(..)
?????全通配方式:
* *..*.*(..)
注: 通常情況下,我們都是對業(yè)務(wù)層的方法進行增強,所以切入點表達式都是切到業(yè)務(wù)層實現(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:定義切入點表達式
- pointcut-ref:指定切入點表達式的引用
- 說明:
- 它是 spring 框架為我們提供的一種可以在代碼中手動控制增強代碼什么時候執(zhí)行的方式。
- 注意:
- 通常情況下,環(huán)繞通知都是獨立使用的
- /**
- * 環(huán)繞通知
- * @param pjp
- * spring 框架為我們提供了一個接口:ProceedingJoinPoint,它可以作為環(huán)繞通知的方法參數(shù)。
- * 在環(huán)繞通知執(zhí)行時,spring 框架會為我們提供該接口的實現(xiàn)類對象,我們直接使用就行。
- * @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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決HttpServletRequest 流數(shù)據(jù)不可重復(fù)讀的操作
這篇文章主要介紹了解決HttpServletRequest 流數(shù)據(jù)不可重復(fù)讀的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08Mybatis如何自動生成數(shù)據(jù)庫表的實體類
這篇文章主要介紹了Mybatis自動生成數(shù)據(jù)庫表的實體類的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06關(guān)于在IDEA熱部署插件JRebel使用問題詳解
這篇文章主要介紹了關(guān)于在IDEA熱部署插件JRebel使用問題詳解,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12JAVA對象中使用?static?和?String?基礎(chǔ)探究
這篇文章主要介紹了JAVA對象中使用static和String基礎(chǔ)探究,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09通過jxl.jar 讀取、導(dǎo)出excel的實例代碼
通過jxl.jar 讀取、導(dǎo)出excel的實例代碼,需要的朋友可以參考一下2013-03-03java實現(xiàn)ThreadLocal線程局部變量的實現(xiàn)
本文主要介紹了java實現(xiàn)ThreadLocal線程局部變量的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07