4.利用AOP實(shí)現(xiàn)自定義Spring注解

AOP基本概念

  • 切面(Aspect) :通知和切入點(diǎn)共同組成了切面粘昨,時(shí)間、地點(diǎn)和要發(fā)生的“故事”。
  • 連接點(diǎn)(Joinpoint) :程序能夠應(yīng)用通知的一個(gè)“時(shí)機(jī)”,這些“時(shí)機(jī)”就是連接點(diǎn)音比,例如方法被調(diào)用時(shí)、異常被拋出時(shí)等等氢惋。
  • 通知(Advice) :通知定義了切面是什么以及何時(shí)使用。描述了切面要完成的工作和何時(shí)需要執(zhí)行這個(gè)工作稽犁。
  • 切入點(diǎn)(Pointcut) :通知定義了切面要發(fā)生的“故事”和時(shí)間焰望,那么切入點(diǎn)就定義了“故事”發(fā)生的地點(diǎn),例如某個(gè)類或方法的名稱已亥。
  • 目標(biāo)對象(Target Object) :即被通知的對象熊赖。

AOP代理(AOP Proxy) 在Spring AOP中有兩種代理方式,JDK動態(tài)代理和CGLIB代理虑椎。默認(rèn)情況下震鹉,TargetObject實(shí)現(xiàn)了接口時(shí),則采用JDK動態(tài)代理捆姜;反之传趾,采用CGLIB代理。
織入(Weaving)把切面應(yīng)用到目標(biāo)對象來創(chuàng)建新的代理對象的過程泥技,織入一般發(fā)生在如下幾個(gè)時(shí)機(jī):
1)編譯時(shí):當(dāng)一個(gè)類文件被編譯時(shí)進(jìn)行織入浆兰,這需要特殊的編譯器才能做到,例如AspectJ的織入編譯器珊豹;
2)類加載時(shí):使用特殊的ClassLoader在目標(biāo)類被加載到程序之前增強(qiáng)類的字節(jié)代碼簸呈;
3)運(yùn)行時(shí):切面在運(yùn)行的某個(gè)時(shí)刻被織入,SpringAOP就是以這種方式織入切面的店茶,原理是使用了JDK的動態(tài)代理蜕便。

AOP通知類型

@Before前置通知(Before advice) :在某連接點(diǎn)(JoinPoint)之前執(zhí)行的通知,但這個(gè)通知不能阻止連接點(diǎn)前的執(zhí)行贩幻。
@After 后通知(After advice) :當(dāng)某連接點(diǎn)退出的時(shí)候執(zhí)行的通知(不論是正常返回還是異常退出)轿腺。
@AfterReturning 返回后通知(After return advice) :在某連接點(diǎn)正常完成后執(zhí)行的通知,不包括拋出異常的情況丛楚。
@Around 環(huán)繞通知(Around advice) :包圍一個(gè)連接點(diǎn)的通知吃溅,類似Web中Servlet規(guī)范中的Filter的doFilter方法⊙烀剩可以在方法的調(diào)用前后完成自定義的行為决侈,也可以選擇不執(zhí)行。
@AfterThrowing 拋出異常后通知(After throwing advice) : 在方法拋出異常退出時(shí)執(zhí)行的通知。

自定義Spring注解實(shí)現(xiàn)

定義注解有好幾種實(shí)現(xiàn)方式赖歌,有使用aop實(shí)現(xiàn)的枉圃,也有使用攔截器實(shí)現(xiàn)的,實(shí)現(xiàn)的話有可能各自有的優(yōu)缺點(diǎn)庐冯,不過總的來說孽亲,還是推薦使用aop方式實(shí)現(xiàn)。

定義注解類

/*
 * Copyright 2002-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package net.itaem.annotation;
 
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
 
/**
 * 說明:
 * 定義日志注解
 * 
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FileLog {
    String value() default "日志記錄開始";
}

定義注解的處理程序

/*
 * Copyright 2002-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package net.itaem.aspect;
 
import net.itaem.annotation.FileLog;
 
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
 
 
/**
 * 說明:
 * 日志的切面處理程序
 * 
 */
@Aspect
@Component
public class LogAspect {
 
    @AfterReturning("within(net.itaem..*) && @annotation(fl)")  
    public void addSuccessLog(JoinPoint jp, FileLog fl){  
        Object[] parames = jp.getArgs();
        //獲取目標(biāo)方法體參數(shù)  
        String className = jp.getTarget().getClass().toString();
        //獲取目標(biāo)類名  
        String signature = jp.getSignature().toString();
        //獲取目標(biāo)方法簽名  
        String methodName = signature.substring(signature.lastIndexOf(".")+1, signature.indexOf("(")); 
        //獲取注解值
        String desc=fl.value();
        //把調(diào)用的信息寫到日常記錄信息里面去...
    }  
  
    //標(biāo)注該方法體為異常通知展父,當(dāng)目標(biāo)方法出現(xiàn)異常時(shí)返劲,執(zhí)行該方法體  
    @AfterThrowing(pointcut="within(net.itaem..*) && @annotation(fl)", throwing="e")  
    public void addExceptionLog(JoinPoint jp, FileLog fl, Exception e){  
       //把錯(cuò)誤信息寫到錯(cuò)誤日志文件里面去...
    }  
}

編寫測試用例

配置spring啟動xml,配置如下

<?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-3.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  ">
        
    <aop:aspectj-autoproxy/>  
    <bean name="purchaseService"   class="net.itaem.test.PurchaseService" />
    <bean class="net.itaem.aspect.LogAspect"/>    
</beans>

編寫service類

public class PurchaseService {
    public void purchaseProduct(String productName,int price,String type) {
        System.out.println("購買商品栖茉。篮绿。。");
    }
}

編寫測試代碼

package net.itaem.test;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class Test {
 
    @org.junit.Test
    public void test() {
        ApplicationContext context=new ClassPathXmlApplicationContext("net/itaem/source/test.xml");
        PurchaseService service=(PurchaseService) context.getBean("purchaseService");
        service.purchaseProduct("電風(fēng)扇", 98, "日用品");
    }
     
}

結(jié)果展示

image.png

總結(jié)

個(gè)人覺得這個(gè)實(shí)現(xiàn)起來比較簡答吕漂, 也比較容易理解亲配,當(dāng)然其他的更深入的擴(kuò)展,還是要慢慢的去學(xué)習(xí)惶凝,探索吼虎,這里只是簡單的做了一個(gè)實(shí)現(xiàn)注解的方式介紹。其實(shí)學(xué)習(xí)的這種狀態(tài)挺快樂的苍鲜,無形中有一種自己又升華了的感覺思灰,好好加油吧~

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市混滔,隨后出現(xiàn)的幾起案子官辈,更是在濱河造成了極大的恐慌,老刑警劉巖遍坟,帶你破解...
    沈念sama閱讀 219,366評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件拳亿,死亡現(xiàn)場離奇詭異,居然都是意外死亡愿伴,警方通過查閱死者的電腦和手機(jī)肺魁,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,521評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來隔节,“玉大人鹅经,你說我怎么就攤上這事≡踅耄” “怎么了瘾晃?”我有些...
    開封第一講書人閱讀 165,689評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長幻妓。 經(jīng)常有香客問我蹦误,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,925評論 1 295
  • 正文 為了忘掉前任强胰,我火速辦了婚禮舱沧,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘偶洋。我一直安慰自己熟吏,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,942評論 6 392
  • 文/花漫 我一把揭開白布玄窝。 她就那樣靜靜地躺著牵寺,像睡著了一般。 火紅的嫁衣襯著肌膚如雪恩脂。 梳的紋絲不亂的頭發(fā)上帽氓,一...
    開封第一講書人閱讀 51,727評論 1 305
  • 那天,我揣著相機(jī)與錄音东亦,去河邊找鬼。 笑死唬渗,一個(gè)胖子當(dāng)著我的面吹牛典阵,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播镊逝,決...
    沈念sama閱讀 40,447評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼壮啊,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了撑蒜?” 一聲冷哼從身側(cè)響起歹啼,我...
    開封第一講書人閱讀 39,349評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎座菠,沒想到半個(gè)月后狸眼,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,820評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡浴滴,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,990評論 3 337
  • 正文 我和宋清朗相戀三年拓萌,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片升略。...
    茶點(diǎn)故事閱讀 40,127評論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡微王,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出品嚣,到底是詐尸還是另有隱情炕倘,我是刑警寧澤,帶...
    沈念sama閱讀 35,812評論 5 346
  • 正文 年R本政府宣布翰撑,位于F島的核電站罩旋,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜瘸恼,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,471評論 3 331
  • 文/蒙蒙 一劣挫、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧东帅,春花似錦压固、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,017評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至愧膀,卻和暖如春拦键,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背檩淋。 一陣腳步聲響...
    開封第一講書人閱讀 33,142評論 1 272
  • 我被黑心中介騙來泰國打工芬为, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人蟀悦。 一個(gè)月前我還...
    沈念sama閱讀 48,388評論 3 373
  • 正文 我出身青樓媚朦,卻偏偏與公主長得像,于是被迫代替她去往敵國和親日戈。 傳聞我的和親對象是個(gè)殘疾皇子询张,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,066評論 2 355

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