国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Spring AOP注解案例及基本原理詳解

瀏覽:3日期:2023-08-31 08:05:42

切面:Aspect

切面=切入點(diǎn)+通知。在老的spring版本中通常用xml配置,現(xiàn)在通常是一個(gè)類帶上@Aspect注解。切面負(fù)責(zé)將 橫切邏輯(通知) 編織 到指定的連接點(diǎn)中。

目標(biāo)對(duì)象:Target

將要被增強(qiáng)的對(duì)象。

連接點(diǎn):JoinPoint

可以被攔截到的程序執(zhí)行點(diǎn),在spring中就是類中的方法。

切入點(diǎn):PointCut

需要執(zhí)行攔截的方法,也就是具體實(shí)施了橫切邏輯的方法。切入點(diǎn)的規(guī)則在spring中通過(guò)AspectJ pointcut expression language來(lái)描述。

切入點(diǎn)與連接點(diǎn)的區(qū)別:連接點(diǎn)是所有可以被'切'的點(diǎn);切入點(diǎn)是真正要切的點(diǎn)。

通知:Advice

針對(duì)切入點(diǎn)的橫切邏輯,包含“around”、“before”和“after”等不同類型的通知。

通知的作用點(diǎn)如其命名:

before:在切入點(diǎn)之前執(zhí)行 after:在切入點(diǎn)之后執(zhí)行 around:在切入點(diǎn)攔截方法,自定義前后,更靈活

還有一些異常處理的通知,這里不一一舉例

織入:Weaving

將切面和目標(biāo)對(duì)象連接起來(lái),創(chuàng)建代理對(duì)象的過(guò)程。spring中用的是動(dòng)態(tài)代理。假如目標(biāo)對(duì)象有接口,使用jdk動(dòng)態(tài)代理;否則使用cglib動(dòng)態(tài)代理。

說(shuō)了這么多概念,看看代碼實(shí)現(xiàn)可能會(huì)使讀者理解的更深刻一些,這里簡(jiǎn)單寫一個(gè)通過(guò)注解增強(qiáng)方法的AOP-Demo。首先是切面類:

package com.example.demo.aop;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.*;import org.springframework.stereotype.Component;/** * @author Fcb * @date 2020/6/20 * @description 切面類=切入點(diǎn)+通知 */@Aspect@Componentpublic class LogAspect { //這個(gè)方法定義了切入點(diǎn) @Pointcut('@annotation(com.example.demo.aop.anno.MyLog)') public void pointCut() {} //這個(gè)方法定義了具體的通知 @After('pointCut()') public void recordRequestParam(JoinPoint joinPoint) { for (Object s : joinPoint.getArgs()) { //打印所有參數(shù),實(shí)際中就是記錄日志了 System.out.println('after advice : ' + s); } } //這個(gè)方法定義了具體的通知 @Before('pointCut()') public void startRecord(JoinPoint joinPoint) { for (Object s : joinPoint.getArgs()) { //打印所有參數(shù) System.out.println('before advice : ' + s); } } //這個(gè)方法定義了具體的通知 @Around('pointCut()') public Object aroundRecord(ProceedingJoinPoint pjp) throws Throwable { for (Object s : pjp.getArgs()) { //打印所有參數(shù) System.out.println('around advice : ' + s); } return pjp.proceed(); }}

注解:

package com.example.demo.aop.anno;import java.lang.annotation.*;/** * @author Fcb * @date 2020/6/20 * @description */@Documented@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.METHOD, ElementType.TYPE})public @interface MyLog {}

目標(biāo)類:

package com.example.demo.aop.target;import com.example.demo.aop.anno.MyLog;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;/** * @author Fcb * @date 2020/6/20 * @description */@RestControllerpublic class MockController { @RequestMapping('/hello') @MyLog public String helloAop(@RequestParam String key) { System.out.println('do something...'); return 'hello world'; }}

最后是測(cè)試類:

package com.example.demo.aop.target;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;/** * @author Fcb * @date 2020/6/20 * @description */@SpringBootTestclass MockControllerTest { @Autowired MockController mockController; @Test void helloAop() { mockController.helloAop('aop'); }}

控制臺(tái)結(jié)果:

around advice : aopbefore advice : aopdo something...after advice : aop

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 龙州县| 怀柔区| 平潭县| 繁昌县| 昭通市| 章丘市| 西吉县| 天镇县| 仁怀市| 巴楚县| 东乌珠穆沁旗| 喜德县| 邵东县| 赤壁市| 九江县| 泾源县| 湘西| 古丈县| 万全县| 株洲县| 红河县| 合川市| 桃园县| 东丽区| 延吉市| 涡阳县| 徐闻县| 威远县| 醴陵市| 集贤县| 科尔| 淮滨县| 佛山市| 闻喜县| 甘孜| 沁源县| 平凉市| 当涂县| 汶上县| 凉城县| 沂水县|