欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot使用AOP記錄接口操作日志詳解

 更新時(shí)間:2022年08月28日 14:37:47   作者:胡蘿卜★  
這篇文章主要為大家詳細(xì)介紹了SpringBoot使用AOP記錄接口操作日志,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

SpringBoot 使用 AOP 記錄接口操作日志,供大家參考,具體內(nèi)容如下

一、AOP簡(jiǎn)介

1.什么是AOP

AOP:Aspect Oriented Programming 面向切面編程

AOP關(guān)注不是某一個(gè)類或某些方法;控制大量資源,關(guān)注的是大量的類和方法。

2.AOP應(yīng)用場(chǎng)景以及常用術(shù)語

  • 權(quán)限控制、緩存控制、事務(wù)控制、分布式追蹤、異常處理等
  • Target:目標(biāo)類,即需要被代理的類。例如:UserService
  • Joinpoint(連接點(diǎn)):所謂連接點(diǎn)是指那些可能被攔截到的方法。例如:所有的方法
  • PointCut 切入點(diǎn):已經(jīng)被增強(qiáng)的連接點(diǎn)。例如:addUser()
  • Advice 通知/增強(qiáng),增強(qiáng)代碼。例如:after、before
  • Weaving(織入):是指把增強(qiáng)advice應(yīng)用到目標(biāo)對(duì)象target來創(chuàng)建新的代理對(duì)象proxy的過程.
  • Aspect(切面): 是切入點(diǎn)pointcut和通知advice的結(jié)合

3.AOP的特點(diǎn)

1)降低模塊與模塊之間的耦合度,提高業(yè)務(wù)代碼的聚合度。(高內(nèi)聚低耦合)

2)提高了代碼的復(fù)用性

3)提高系統(tǒng)的擴(kuò)展性。(高版本兼容低版本)

4)可以在不影響原有的功能基礎(chǔ)上添加新的功能

二、springBoot 使用 AOP 實(shí)現(xiàn)流程

1.引入依賴

<!-- Spring AOP -->
? <dependency>
? ? ? <groupId>org.springframework.boot</groupId>
? ? ? <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

2.封裝記錄日志實(shí)體類

@Getter
@Setter
@ApiModel(value = "Systemlog對(duì)象", description = "")
public class Systemlog implements Serializable {

? ? private static final long serialVersionUID = 1L;

? ? ? @ApiModelProperty("ID")
? ? ? @TableId(value = "id", type = IdType.AUTO)
? ? ? private Integer id;

? ? ? @ApiModelProperty("用戶名")
? ? ? private String userName;

? ? ? @ApiModelProperty("用戶ID")
? ? ? private Integer userId;

? ? ? @ApiModelProperty("操作描述")
? ? ? private String operate;

? ? ? @ApiModelProperty("模塊")
? ? ? private String module;

? ? ? @ApiModelProperty("創(chuàng)建日志時(shí)間")
? ? ? @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
? ? ? private Date createTime;

? ? ? @ApiModelProperty("操作結(jié)果")
? ? ? private String result;

}

3.編寫注解類(自定義日志注解類)

/**
?* controller層切面日志注解
?* @author hsq
?*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SystemControllerLog {
? ? // 操作描述
? ? String operate();
? ? // 模塊
? ? String module();
}

4.編寫操作日志的切面類

**
?* @author hsq
?*/
@Aspect
@Component
public class SystemLogAspect {

? ? private static final Logger logger = LoggerFactory.getLogger(SystemLogAspect.class);

? ? @Autowired
? ? private ISystemlogService iSystemlogService;

? ? @Autowired
? ? private UserService userService;

? ? /**
? ? ?* Controller層切點(diǎn)
? ? ?*/
? ? @Pointcut("@annotation(com.hsq.demo.config.SystemControllerLog)")
? ? public void SystemControllerLog(){

? ? }
? ??
? ?
? ? /**
? ? ?* 前置通知 用于攔截Controller層記錄用戶的操作的開始時(shí)間
? ? ?* @param joinPoint 切點(diǎn)
? ? ?* @throws InterruptedException
? ? ?*/
? ? @Before("SystemControllerLog()")
? ? public void doBefore(JoinPoint joinPoint) throws InterruptedException{
? ? ? ? logger.info("進(jìn)入日志切面前置通知!");

? ? }

? ? @After("SystemControllerLog()")
? ? public void doAfter(JoinPoint joinPoint) {
? ? ? ? logger.info("進(jìn)入日志切面后置通知!");

? ? }

? ? /**value切入點(diǎn)位置
? ? ?* returning 自定義的變量,標(biāo)識(shí)目標(biāo)方法的返回值,自定義變量名必須和通知方法的形參一樣
? ? ?* 特點(diǎn):在目標(biāo)方法之后執(zhí)行的,能夠獲取到目標(biāo)方法的返回值,可以根據(jù)這個(gè)返回值做不同的處理
? ? ?*/
? ? @AfterReturning(value = "SystemControllerLog()", returning = "ret")
? ? public void doAfterReturning(Object ret) throws Throwable {
? ? }

? ? /***
? ? ?* 異常通知 記錄操作報(bào)錯(cuò)日志
? ? ?* * @param joinPoint
? ? ?* * @param e
? ? ?* */
? ? @AfterThrowing(pointcut = "SystemControllerLog()", throwing = "e")
? ? public void doAfterThrowing(JoinPoint joinPoint, Throwable e) {
? ? ? ? logger.info("進(jìn)入日志切面異常通知!!");
? ? ? ? logger.info("異常信息:" + e.getMessage());
? ? }
?? ??? ?
?? ??? ?
?? ?//使用這個(gè)方法先注釋前面三個(gè)方法,留before方法就行
? ? /**
? ? ?* 通知包裹了目標(biāo)方法,在目標(biāo)方法調(diào)用之前和之后執(zhí)行自定義的行為
? ? ?* ProceedingJoinPoint切入點(diǎn)可以獲取切入點(diǎn)方法上的名字、參數(shù)、注解和對(duì)象
? ? ?* @param joinPoint
? ? ?*/
? ? @Around("SystemControllerLog() && @annotation(systemControllerLog)")
? public Result doAfterReturning(ProceedingJoinPoint joinPoint, SystemControllerLog systemControllerLog) throws Throwable {
? ? ? ? logger.info("設(shè)置日志信息存儲(chǔ)到表中!");
? ? ? ? //joinPoint.proceed() 結(jié)果集
? ? ? ? //參數(shù)數(shù)組
? ? ? ? Object[] args = joinPoint.getArgs();
? ? ? ? //請(qǐng)求參數(shù)數(shù)據(jù)
? ? ? ? String requestJson = JSONUtil.toJsonStr(args);
? ? ? ? //方法名
? ? ? ? String methodName = joinPoint.getSignature().getName();
? ? ? ? //得到request
? ? ? ? HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
? ? ? ? //得到token
? ? ? ? String token = request.getHeader("token");
? ? ? ? String userId = JWT.decode(token).getAudience().get(0);
? ? ? ? User user = userService.getById(userId);
? ? ? ? logger.info("得到用戶信息:"+user.toString());
? ? ? ? //寫入數(shù)據(jù)庫操作日志
? ? ? ? Systemlog systemlog = new Systemlog();
? ? ? ? systemlog.setUserId(user.getUid());
? ? ? ? systemlog.setUserName(user.getUname());
? ? ? ? systemlog.setOperate(systemControllerLog.operate());
? ? ? ? systemlog.setModule(systemControllerLog.module());
? ? ? ? systemlog.setCreateTime(new Date());
? ? ? ? //存入返回的結(jié)果集 joinPoint.proceed()
? ? ? ? Result proceed = (Result) joinPoint.proceed();
? ? ? ? systemlog.setResult(JSONUtil.toJsonStr(joinPoint.proceed()));
? ? ? ? //保存
? ? ? ?saveSystemLog(systemlog);

? ? ? ?return proceed;

? ? }

}

5.controller使用

?@GetMapping("/userListPage")
?@SystemControllerLog(operate = "用戶查詢",module = "用戶管理")
?public Result findUserList( @RequestParam Integer pageNum,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? @RequestParam Integer pageSize,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? @RequestParam String username,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? @RequestParam String loveValue,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? @RequestParam String address) {}
? @PostMapping("/addOrUpdate")
? @SystemControllerLog(operate = "用戶修改或者添加",module = "用戶管理")
? public Result addOrUpdateUser(@RequestBody User user){}

6.數(shù)據(jù)庫記錄

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

相關(guān)文章

  • Java中關(guān)于char類型變量能夠輸出中文的問題

    Java中關(guān)于char類型變量能夠輸出中文的問題

    這篇文章主要介紹了Java中關(guān)于char類型變量能夠輸出中文的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Java學(xué)生信息類繼承與接口的原理及使用方式

    Java學(xué)生信息類繼承與接口的原理及使用方式

    這篇文章主要介紹了Java學(xué)生信息類繼承與接口的原理及使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Java基于TCP協(xié)議的Socket通信

    Java基于TCP協(xié)議的Socket通信

    本文詳細(xì)講解了Java基于TCP協(xié)議的Socket通信,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-12-12
  • MybatisPlus:使用SQL保留字(關(guān)鍵字)的操作

    MybatisPlus:使用SQL保留字(關(guān)鍵字)的操作

    這篇文章主要介紹了MybatisPlus:使用SQL保留字(關(guān)鍵字)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • 解決springboot使用logback日志出現(xiàn)LOG_PATH_IS_UNDEFINED文件夾的問題

    解決springboot使用logback日志出現(xiàn)LOG_PATH_IS_UNDEFINED文件夾的問題

    這篇文章主要介紹了解決springboot使用logback日志出現(xiàn)LOG_PATH_IS_UNDEFINED文件夾的問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • 詳解Java中static關(guān)鍵字和內(nèi)部類的使用

    詳解Java中static關(guān)鍵字和內(nèi)部類的使用

    這篇文章主要為大家詳細(xì)介紹了Java中static關(guān)鍵字和內(nèi)部類的使用,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2022-08-08
  • Redis監(jiān)聽過期的key實(shí)現(xiàn)流程詳解

    Redis監(jiān)聽過期的key實(shí)現(xiàn)流程詳解

    本文主要介紹了Redis監(jiān)聽key的過期時(shí)間,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • Java如何實(shí)現(xiàn)簡(jiǎn)單后臺(tái)訪問并獲取IP

    Java如何實(shí)現(xiàn)簡(jiǎn)單后臺(tái)訪問并獲取IP

    這篇文章主要介紹了Java如何實(shí)現(xiàn)簡(jiǎn)單后臺(tái)訪問并獲取IP,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • IntelliJ IDEA2021.1 配置大全(超詳細(xì)教程)

    IntelliJ IDEA2021.1 配置大全(超詳細(xì)教程)

    這篇文章主要介紹了IntelliJ IDEA2021.1 配置大全(超詳細(xì)教程),需要的朋友可以參考下
    2021-04-04
  • 基于Springboot實(shí)現(xiàn)JWT認(rèn)證的示例代碼

    基于Springboot實(shí)現(xiàn)JWT認(rèn)證的示例代碼

    本文主要介紹了基于Springboot實(shí)現(xiàn)JWT認(rèn)證,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11

最新評(píng)論