最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

一、前言

我們通過Spring AOP在每次執(zhí)行方法前或執(zhí)行方法后進行切面的處理,進而統(tǒng)計方法訪問的次數(shù)等功能,可以持久化到數(shù)據(jù)庫或者后面進行接口并發(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>

三、項目配置

我們主要添加數(shù)據(jù)庫相關(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)建自定義計數(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)建實體類和持久層

package com.example.aopdemo.entity;

import javax.persistence.*;

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

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

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

    /**
     * 請求路徑
     */
    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 訪問請求持久層
 */
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)前請求的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";
    }
}

八、測試

啟動程序,在瀏覽器上訪問請求地址。

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

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

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

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

相關(guān)文章

  • 基于jdbc處理Clob的使用介紹

    基于jdbc處理Clob的使用介紹

    本篇文章是對jdbc處理Clob的使用進行了分析介紹,需要的朋友參考下
    2013-05-05
  • JavaMe開發(fā)自適應(yīng)滾動顯示

    JavaMe開發(fā)自適應(yīng)滾動顯示

    我們??吹揭恍L動顯示的實例,比如UC瀏覽器中,顯示網(wǎng)頁的內(nèi)容。當(dāng)內(nèi)容比較多時,采用滾動分頁顯示是合理的。在Canvas中繪圖中,多余的內(nèi)容被截斷了。如何實現(xiàn)滾動分頁顯示呢?
    2015-09-09
  • java工廠實例BeanFactoryPostProcessor和BeanPostProcessor區(qū)別分析

    java工廠實例BeanFactoryPostProcessor和BeanPostProcessor區(qū)別分析

    這篇文章主要為大家介紹了BeanFactoryPostProcessor和BeanPostProcessor區(qū)別示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • SpringBoot 整合 Netty 多端口監(jiān)聽的操作方法

    SpringBoot 整合 Netty 多端口監(jiān)聽的操作方法

    Netty提供異步的、基于事件驅(qū)動的網(wǎng)絡(luò)應(yīng)用程序框架,用以快速開發(fā)高性能、高可靠性的網(wǎng)絡(luò) IO 程序,是目前最流行的 NIO 框架,這篇文章主要介紹了SpringBoot 整和 Netty 并監(jiān)聽多端口,需要的朋友可以參考下
    2023-10-10
  • JDK的下載、安裝與部署圖文教程

    JDK的下載、安裝與部署圖文教程

    這篇文章主要為大家詳細(xì)介紹了JDK的下載、安裝與部署圖文教程,感興趣的小伙伴們可以參考一下
    2016-06-06
  • Java字節(jié)碼操縱框架ASM圖文實例詳解

    Java字節(jié)碼操縱框架ASM圖文實例詳解

    這篇文章主要為大家介紹了Java字節(jié)碼操縱框架ASM圖文實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • 淺談Java方法的重載

    淺談Java方法的重載

    方法重載是指在一個類中定義多個同名的方法,但要求每個方法具有不同的參數(shù)的類型或參數(shù)的個數(shù)。調(diào)用重載方法時,Java編譯器能通過檢查調(diào)用的方法的參數(shù)類型和個數(shù)選擇一個恰當(dāng)?shù)姆椒?。方法重載通常用于創(chuàng)建完成一組任務(wù)相似但參數(shù)的類型或參數(shù)的個數(shù)不同的方法。
    2016-04-04
  • 全面了解Java中的內(nèi)部類和匿名類

    全面了解Java中的內(nèi)部類和匿名類

    下面小編就為大家?guī)硪黄媪私釰ava中的內(nèi)部類和匿名類。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-07-07
  • Java排序算法中的快速排序算法實現(xiàn)

    Java排序算法中的快速排序算法實現(xiàn)

    這篇文章主要介紹了Java排序算法中的快速排序算法實現(xiàn),通過一趟排序?qū)⒁判虻臄?shù)據(jù)分割成獨立的兩部分,其中一部分的所有數(shù)據(jù)都比另外一部分的所有數(shù)據(jù)都要小,然后再按此方法對這兩部分?jǐn)?shù)據(jù)分別進行快速排序,需要的朋友可以參考下
    2023-12-12
  • 修改Zookeeper的客戶端連接端口方式(默認(rèn)2181端口)

    修改Zookeeper的客戶端連接端口方式(默認(rèn)2181端口)

    本文介紹了Zookeeper配置文件zoo.cfg的基本配置和修改客戶端連接端口的方法,特別強調(diào)了server.x配置項中三個端口的含義,并給出修改后的zoo.cfg文件作為參考
    2026-04-04

最新評論

勐海县| 海晏县| 岫岩| 都昌县| 元谋县| 明星| 宽城| 田林县| 仁寿县| 德昌县| 肇庆市| 监利县| 明星| 青神县| 定陶县| 紫云| 普陀区| 微山县| 安陆市| 珠海市| 茂名市| 齐齐哈尔市| 麦盖提县| 务川| 黄平县| 莱西市| 兴隆县| 白朗县| 湖州市| 巫山县| 龙井市| 榆社县| 会理县| 迁西县| 罗田县| 墨竹工卡县| 根河市| 咸宁市| 舟曲县| 巩义市| 新宁县|