久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

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

SpringBoot基于自定義注解實(shí)現(xiàn)切面編程

瀏覽:45日期:2023-04-07 10:44:33

1、相關(guān)依賴包

<!-- aop 依賴包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.6</version> </dependency>

2、定義切面類

package com.bz.aspect;import com.bz.service.SysLogOperationService;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.*;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;/** * 操作日志,切面處理類 */@Aspect@Componentpublic class LogOperationAspect { @Autowired(required = false) private SysLogOperationService sysLogOperationService; @Pointcut('@annotation(com.bz.aspect.BzLogOperation)') public void logPointCut() { System.out.println('lllll'); } /** * 前置通知:方法執(zhí)行前調(diào)用 */ @Before('logPointCut()') public void begin() { System.out.println('前置通知:方法執(zhí)行前調(diào)用'); } /** * 后置通知: 方法執(zhí)行后調(diào)用,若方法出現(xiàn)異常,不執(zhí)行 */ @AfterReturning('logPointCut()') public void afterReturning() { System.out.println('后置通知: 方法執(zhí)行后調(diào)用,若方法出現(xiàn)異常,不執(zhí)行'); } /** * 最終通知:無論無何都會(huì)調(diào)用,類似于:try/catch中的finally */ @After('logPointCut()') public void after() { System.out.println('最終通知:無論無何都會(huì)調(diào)用,類似于:try/catch中的finally'); } /** * 異常通知:方法拋出異常時(shí)執(zhí)行 */ @AfterThrowing('logPointCut()') public void afterThrowing() { System.out.println('異常通知:方法拋出異常時(shí)執(zhí)行'); } /** * 環(huán)繞通知 * 既可以在目標(biāo)方法之前織入增強(qiáng)動(dòng)作,也可以在執(zhí)行目標(biāo)方法之后織入增強(qiáng)動(dòng)作; * 可以決定目標(biāo)方法在什么時(shí)候執(zhí)行,如何執(zhí)行,甚至可以完全阻止目標(biāo)目標(biāo)方法的執(zhí)行; * 可以改變執(zhí)行目標(biāo)方法的參數(shù)值,也可以改變執(zhí)行目標(biāo)方法之后的返回值; 當(dāng)需要改變目標(biāo)方法的返回值時(shí),只能使用Around方法; */ @Around('logPointCut()') public void around(ProceedingJoinPoint point) throws Throwable { // 獲取切點(diǎn)方法的名稱 String methodName = point.getSignature().getName(); // 獲取方法傳入?yún)?shù) Object[] params = point.getArgs(); //轉(zhuǎn)成字符串 List<Object> objects = Arrays.asList(params); objects.forEach(obj -> System.out.println(JSON.toJSONString(obj))); System.out.println('環(huán)繞通知'); }}

3、自定義切面注解類

package com.bz.aspect;import java.lang.annotation.*;/** * @author: BuZheng * @date: 2020-05-18 下午 2:02 */@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface BzLogOperation { String value() default '';}

4、接口測(cè)試

@ApiOperation('切面測(cè)試') @GetMapping('/aop') @BzLogOperation('切面測(cè)試') public ResultBean userList(@RequestParam(value = 'keyWord') String keyWord) { log.info('### 切面測(cè)試 ###'); return new ResultBean(); }

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

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 博野县| 莒南县| 苍南县| 萨迦县| 郓城县| 安宁市| 隆昌县| 金门县| 光山县| 洞头县| 临夏市| 眉山市| 馆陶县| 阿坝县| 苏尼特右旗| 新竹市| 正安县| 普陀区| 石阡县| 阿勒泰市| 滨州市| 青龙| 阿拉善右旗| 儋州市| 柏乡县| 大丰市| 永胜县| 泾川县| 嘉善县| 含山县| 磐安县| 绍兴市| 丹东市| 娄烦县| 呼伦贝尔市| 庆城县| 玛纳斯县| 阿坝| 太白县| 峡江县| 多伦县|