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

SpringBoot?Aop實(shí)現(xiàn)接口請(qǐng)求次數(shù)統(tǒng)計(jì)

 更新時(shí)間:2024年02月06日 14:44:20   作者:wx59bcc77095d22  
我們通過Spring AOP在每次執(zhí)行方法前或執(zhí)行方法后進(jìn)行切面的處理,進(jìn)而統(tǒng)計(jì)方法訪問的次數(shù)等功能,本文主要介紹了SpringBoot?Aop實(shí)現(xiàn)接口請(qǐng)求次數(shù)統(tǒng)計(jì)

一、前言

我們通過Spring AOP在每次執(zhí)行方法前或執(zhí)行方法后進(jìn)行切面的處理,進(jìn)而統(tǒng)計(jì)方法訪問的次數(shù)等功能,可以持久化到數(shù)據(jù)庫(kù)或者后面進(jìn)行接口并發(fā)的處理。

二、添加依賴

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.31</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

三、項(xiàng)目配置

我們主要添加數(shù)據(jù)庫(kù)相關(guān)的配置。

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/aop_db?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

四、創(chuàng)建自定義計(jì)數(shù)注解

package com.example.aopdemo.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 訪問次數(shù)注解
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface VisitCount {

}

五、創(chuàng)建實(shí)體類和持久層

package com.example.aopdemo.entity;

import javax.persistence.*;

/**
 * @author qx
 * @date 2024/2/4
 * @des 訪問情況實(shí)體
 */
@Entity
@Table(name = "t_visit")
public class Visit {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 方法名稱
     */
    private String methodName;

    /**
     * 請(qǐng)求路徑
     */
    private String url;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}
package com.example.aopdemo.repository;

import com.example.aopdemo.entity.Visit;
import org.springframework.data.jpa.repository.JpaRepository;

/**
 * @author qx
 * @date 2024/2/4
 * @des 訪問請(qǐng)求持久層
 */
public interface VisitRepository extends JpaRepository<Visit, Long> {
}

六、創(chuàng)建切面

package com.example.aopdemo.aspect;

import com.example.aopdemo.entity.Visit;
import com.example.aopdemo.repository.VisitRepository;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @author qx
 * @date 2024/2/4
 * @des 方法訪問切面
 */
@Aspect
@Component
public class VisitCountAspect {

    @Autowired
    private VisitRepository visitRepository;

    @Around("@annotation(com.example.aopdemo.annotation.VisitCount)")
    public Object VisitCountAround(ProceedingJoinPoint joinPoint) throws Throwable {
        // 獲取當(dāng)前請(qǐng)求的URL
        String url = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getRequestURL().toString();
        // 方法名
        String methodName = joinPoint.getSignature().getName();

        // 保存訪問數(shù)據(jù)
        Visit visit = new Visit();
        visit.setUrl(url);
        visit.setMethodName(methodName);
        visitRepository.save(visit);
        return joinPoint.proceed();
    }

}

七、創(chuàng)建控制層并添加注解

package com.example.aopdemo.controller;

import com.example.aopdemo.annotation.VisitCount;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author qx
 * @date 2024/2/4
 * @des
 */
@RestController
@RequestMapping("/index")
public class IndexController {

    @GetMapping("/one")
    @VisitCount
    public String showOne() {
        return "one";
    }

    @GetMapping("/two")
    @VisitCount
    public String showTwo() {
        return "two";
    }
}

八、測(cè)試

啟動(dòng)程序,在瀏覽器上訪問請(qǐng)求地址。

SpringBoot Aop實(shí)現(xiàn)接口請(qǐng)求次數(shù)統(tǒng)計(jì)_spring

我們刷新數(shù)據(jù)庫(kù),發(fā)現(xiàn)新增了數(shù)據(jù)。這樣我們就可以根據(jù)自己的需求獲取請(qǐng)求次數(shù)。

SpringBoot Aop實(shí)現(xiàn)接口請(qǐng)求次數(shù)統(tǒng)計(jì)_aop_02

到此這篇關(guān)于SpringBoot Aop實(shí)現(xiàn)接口請(qǐng)求次數(shù)統(tǒng)計(jì)的文章就介紹到這了,更多相關(guān)SpringBoot Aop接口請(qǐng)求次數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • 詳解Java并發(fā)編程之volatile關(guān)鍵字

    詳解Java并發(fā)編程之volatile關(guān)鍵字

    這篇文章主要為大家介紹了Java并發(fā)編程之volatile關(guān)鍵字,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-11-11
  • Java8中CompletableFuture的用法全解

    Java8中CompletableFuture的用法全解

    這篇文章主要給大家介紹了關(guān)于Java8中CompletableFuture用法的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2022-01-01
  • java開發(fā)分布式服務(wù)框架Dubbo服務(wù)引用過程詳解

    java開發(fā)分布式服務(wù)框架Dubbo服務(wù)引用過程詳解

    這篇文章主要為大家介紹了java開發(fā)分布式服務(wù)框架Dubbo服務(wù)引用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2021-11-11
  • 關(guān)于spring屬性占位符用法

    關(guān)于spring屬性占位符用法

    這篇文章主要介紹了關(guān)于spring屬性占位符用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • java發(fā)送郵件示例講解

    java發(fā)送郵件示例講解

    這篇文章主要為大家詳細(xì)介紹了java發(fā)送郵件示例的全過程,溫習(xí)郵件協(xié)議,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-03-03
  • MyBatis使用<foreach>標(biāo)簽like查詢報(bào)錯(cuò)解決問題

    MyBatis使用<foreach>標(biāo)簽like查詢報(bào)錯(cuò)解決問題

    這篇文章主要介紹了MyBatis使用<foreach>標(biāo)簽like查詢報(bào)錯(cuò)解決問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • mybatis調(diào)用存儲(chǔ)過程的實(shí)例代碼

    mybatis調(diào)用存儲(chǔ)過程的實(shí)例代碼

    這篇文章主要介紹了mybatis調(diào)用存儲(chǔ)過程的實(shí)例,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-10-10
  • 如何通過XML方式配置并實(shí)現(xiàn)Mybatis

    如何通過XML方式配置并實(shí)現(xiàn)Mybatis

    這篇文章主要介紹了如何通過XML方式配置并實(shí)現(xiàn)Mybatis,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • 如何在Spring?Boot中使用OAuth2認(rèn)證和授權(quán)

    如何在Spring?Boot中使用OAuth2認(rèn)證和授權(quán)

    這篇文章主要介紹了如何在Spring?Boot中使用OAuth2認(rèn)證和授權(quán)的相關(guān)資料,OAuth2.0是一種開放的授權(quán)協(xié)議,它允許用戶授權(quán)第三方應(yīng)用訪問其賬戶(或資源),而無需共享其用戶賬戶憑據(jù),需要的朋友可以參考下
    2023-12-12
  • Spring boot整合mybatis實(shí)現(xiàn)過程圖解

    Spring boot整合mybatis實(shí)現(xiàn)過程圖解

    這篇文章主要介紹了Spring boot整合mybatis實(shí)現(xiàn)過程圖解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08

最新評(píng)論