6.Aop操作(基于aspectj的xml方式)

  • aop基本概念
  • aop操作準(zhǔn)備
  • 使用表達(dá)式配置切入點(diǎn)

aop操作基本概念

1 在spring里面進(jìn)行aop操作网严,使用 aspectj(餓死怕死J)實(shí)現(xiàn)
(1)aspectj不是spring一部分识樱,和spring一起使用進(jìn)行aop操作
(2)Spring2.0以后新增了對AspectJ支持
2 使用aspectj實(shí)現(xiàn)aop有兩種方式
(1)基于aspectj的xml配置
(2)基于aspectj的注解方式

aop操作準(zhǔn)備

導(dǎo)入Maven依賴和aop約束

<!-- ioc編程 -->
  <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.4</version>
    </dependency>

使用表達(dá)式配置切入點(diǎn)

1 切入點(diǎn):實(shí)際增強(qiáng)的方法
2 常用的表達(dá)式

execution(<訪問修飾符>?<返回類型><方法名>(<參數(shù)>)<異常>)
(1)execution(* cn.itcast.aop.Book.add(..))
含義:任意修飾符,在cn.itcast.aop包下Book類中add方法(),有參數(shù)也包含
(2)execution(* cn.itcast.aop.Book.(..))
含義:任意修飾符,在cn.itcast.aop包下Book類中所有方法(),有參數(shù)也包含
(3)execution(
.(..))
含義:任意修飾符,在所有類下所有方法,有參數(shù)也包含
(4) 匹配所有save開頭的方法 execution(* save*(..))
含義:任意修飾符,以save開頭的任意方法都增強(qiáng),有參數(shù)也包含

3 AspectJ支持5種類型的通知注解:

@Before:前置通知震束,在方法執(zhí)行之前返回
@After:后置通知怜庸,在方法執(zhí)行后執(zhí)行
@AfterRunning:返回通知,在方法返回結(jié)果之后執(zhí)行
@AfterThrowing:異常通知垢村,在方法拋出異常之后
@Around:環(huán)繞通知割疾,圍繞著方法執(zhí)行

package com.neuedu.aop;

public class Book {

    public void add() {
        System.out.println("add...........");
    }
}
package com.neuedu.aop;

public class MyBook {

    public void before1() {
        System.out.println("前置增強(qiáng)......");
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
                        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
                        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
                        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
                        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
                        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

<!-- 1.配置對象 -->
<bean id="book" class="com.neuedu.aop.Book"></bean>
<bean id="myBook" class="com.neuedu.aop.MyBook"></bean>

<!-- 2.配置Aop操作 -->
<aop:config>
    <!-- 2.1配置切入點(diǎn) -->
    <aop:pointcut expression="execution(* com.neuedu.aop.Book.*(..))" id="qieru"/>
    <!-- 2.2配置切面,把增強(qiáng)用到方法上面 -->
        <aop:aspect ref="myBook">
        <!-- 2.3配置增強(qiáng)類型method:增強(qiáng)類里面使用哪個方法作為前置-->
            <aop:before method="before1" pointcut-ref="qieru"/>
       </aop:aspect>
</aop:config>
</beans>
舉例

測試

其他Aspectj的aop其他操作

package com.neuedu.aop;

import org.aspectj.lang.ProceedingJoinPoint;

public class MyBook {

    public void before1() {
        System.out.println("前置增強(qiáng)......");
    }
    
    public void after1() {
        System.out.println("后置增強(qiáng)......");
    }
    
    //環(huán)繞通知
    public void around1(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        //方法之前
        System.out.println("方法之前...");
        
        //執(zhí)行被增強(qiáng)的方法
        proceedingJoinPoint.proceed();
        
        //方法之后
        System.out.println("方法之后...");
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
                        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
                        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
                        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
                        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
                        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

<!-- 1.配置對象 -->
<bean id="book" class="com.neuedu.aop.Book"></bean>
<bean id="myBook" class="com.neuedu.aop.MyBook"></bean>
<!-- 2.配置Aop操作 -->
<aop:config>
    <!-- 2.1配置切入點(diǎn) -->
    <aop:pointcut expression="execution(* com.neuedu.aop.Book.*(..))" id="qieru"/>
    <!-- 2.2配置切面肝断,把增強(qiáng)用到方法上面 -->
        <aop:aspect ref="myBook">
        <!-- 2.3配置增強(qiáng)類型method:增強(qiáng)類里面使用哪個方法作為前置-->
            <aop:before method="before1" pointcut-ref="qieru"/>
            <!-- 后置增強(qiáng) -->
            <aop:after-returning method="after1" pointcut-ref="qieru"/>
            <!-- 環(huán)繞通知 -->
            <aop:around method="around1" pointcut-ref="qieru"/>
       </aop:aspect>
</aop:config>
</beans>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末杈曲,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子胸懈,更是在濱河造成了極大的恐慌,老刑警劉巖恰响,帶你破解...
    沈念sama閱讀 212,542評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件趣钱,死亡現(xiàn)場離奇詭異胚宦,居然都是意外死亡燕垃,警方通過查閱死者的電腦和手機(jī)井联,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,596評論 3 385
  • 文/潘曉璐 我一進(jìn)店門烙常,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蚕脏,“玉大人,你說我怎么就攤上這事秦驯≌踝兀” “怎么了?”我有些...
    開封第一講書人閱讀 158,021評論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長皂甘。 經(jīng)常有香客問我,道長璧瞬,這世上最難降的妖魔是什么渐夸? 我笑而不...
    開封第一講書人閱讀 56,682評論 1 284
  • 正文 為了忘掉前任瘟忱,我火速辦了婚禮苫幢,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘韩肝。我一直安慰自己,他們只是感情好涡相,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,792評論 6 386
  • 文/花漫 我一把揭開白布催蝗。 她就那樣靜靜地躺著,像睡著了一般丙号。 火紅的嫁衣襯著肌膚如雪槽袄。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,985評論 1 291
  • 那天截酷,我揣著相機(jī)與錄音乾戏,去河邊找鬼。 笑死三幻,一個胖子當(dāng)著我的面吹牛念搬,可吹牛的內(nèi)容都是我干的摆出。 我是一名探鬼主播,決...
    沈念sama閱讀 39,107評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼爷恳,長吁一口氣:“原來是場噩夢啊……” “哼温亲!你這毒婦竟也來了杯矩?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,845評論 0 268
  • 序言:老撾萬榮一對情侶失蹤节芥,失蹤者是張志新(化名)和其女友劉穎头镊,沒想到半個月后魄幕,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,299評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡坛芽,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,612評論 2 327
  • 正文 我和宋清朗相戀三年咙轩,在試婚紗的時候發(fā)現(xiàn)自己被綠了活喊。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,747評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡钾菊,死狀恐怖煞烫,靈堂內(nèi)的尸體忽然破棺而出滞详,到底是詐尸還是另有隱情紊馏,我是刑警寧澤,帶...
    沈念sama閱讀 34,441評論 4 333
  • 正文 年R本政府宣布稀火,位于F島的核電站凰狞,受9級特大地震影響沛慢,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜团甲,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,072評論 3 317
  • 文/蒙蒙 一身腻、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧嘀趟,春花似錦、人聲如沸她按。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,828評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽默伍。三九已至授霸,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間显设,已是汗流浹背辛辨。 一陣腳步聲響...
    開封第一講書人閱讀 32,069評論 1 267
  • 我被黑心中介騙來泰國打工斗搞, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留僻焚,地道東北人。 一個月前我還...
    沈念sama閱讀 46,545評論 2 362
  • 正文 我出身青樓隙弛,卻偏偏與公主長得像全闷,于是被迫代替她去往敵國和親萍启。 傳聞我的和親對象是個殘疾皇子屏鳍,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,658評論 2 350

推薦閱讀更多精彩內(nèi)容