SpringBoot基于自定義注解實(shí)現(xiàn)切面編程
1、相關(guān)依賴(lài)包
<!-- aop 依賴(lài)包 -->
<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、定義切面類(lèi)
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;
/**
* 操作日志,切面處理類(lèi)
*/
@Aspect
@Component
public 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í)行");
}
/**
* 最終通知:無(wú)論無(wú)何都會(huì)調(diào)用,類(lèi)似于:try/catch中的finally
*/
@After("logPointCut()")
public void after() {
System.out.println("最終通知:無(wú)論無(wú)何都會(huì)調(diào)用,類(lèi)似于: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)方法的名稱(chēng)
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、自定義切面注解類(lèi)
package com.bz.aspect;
import java.lang.annotation.*;
/**
* @author: BuZheng
* @date: 2020-05-18 下午 2:02
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java面試題之HashSet的實(shí)現(xiàn)原理
這篇文章主要介紹了Java面試題之HashSet的實(shí)現(xiàn)原理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
關(guān)于mybatis-plus插件使用時(shí)的一些問(wèn)題小結(jié)
這篇文章主要給大家介紹了關(guān)于mybatis-plus插件使用時(shí)的一些問(wèn)題的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-03-03
通過(guò)@Resource注解實(shí)現(xiàn)屬性裝配代碼詳解
這篇文章主要介紹了通過(guò)@Resource注解實(shí)現(xiàn)屬性裝配代碼詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01
Java參數(shù)校驗(yàn)@Validated、@Valid介紹及使用詳解
Javax.validation是?spring?集成自帶的一個(gè)參數(shù)校驗(yàn)接口,可通過(guò)添加注解來(lái)設(shè)置校驗(yàn)條件,這篇文章主要介紹了Java參數(shù)校驗(yàn)@Validated、@Valid介紹及使用詳解,需要的朋友可以參考下2024-08-08
Java線(xiàn)程監(jiān)聽(tīng),意外退出線(xiàn)程后自動(dòng)重啟的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇Java線(xiàn)程監(jiān)聽(tīng),意外退出線(xiàn)程后自動(dòng)重啟的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
Java下利用Jackson進(jìn)行JSON解析和序列化示例
本篇文章主要介紹了Java下利用Jackson進(jìn)行JSON解析和序列化示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02
java實(shí)現(xiàn)兩個(gè)線(xiàn)程交替打印的實(shí)例代碼
在本篇文章里小編給大家整理的是一篇關(guān)于java實(shí)現(xiàn)兩個(gè)線(xiàn)程交替打印的相關(guān)知識(shí)點(diǎn)內(nèi)容,有需要的朋友們參考下。2019-12-12

