亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

淺談springboot之JoinPoint的getSignature方法

 更新時(shí)間:2021年06月18日 09:06:58   作者:Mint6  
這篇文章主要介紹了springboot之JoinPoint的getSignature方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

JoinPoint的getSignature方法

在使用springboot寫aop的時(shí)候,有個(gè)JoinPoint類,用來獲取代理類和被代理類的信息。

這個(gè)文章記錄一下JoinPoint的getSignature方法返回的是什么格式。

不廢話,貼代碼

package org.aspectj.lang; 
public interface Signature {
    String toString(); 
    String toShortString(); 
    String toLongString(); 
    String getName(); 
    int getModifiers(); 
    Class getDeclaringType(); 
    String getDeclaringTypeName();
}

打印輸出,getString是測(cè)試類的方法名,TestController是類名

joinPoint.getSignature().toString():String com.fast.web.controller.TestController.getString()
joinPoint.getSignature().toShortString():TestController.getString()
joinPoint.getSignature().toLongString():public java.lang.String com.fast.web.controller.TestController.getString()
joinPoint.getSignature().getName():getString
joinPoint.getSignature().getModifiers():1
joinPoint.getSignature().getDeclaringType():class com.fast.web.controller.TestController
joinPoint.getSignature().getDeclaringTypeName():com.fast.web.controller.TestController

冒號(hào)前面是使用的方法,后面是本次測(cè)試輸出的結(jié)果。

附上被測(cè)試的類:

package com.fast.web.controller;
import com.fast.framework.dao.TestDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; 
@RestController
public class TestController {
 
    @Autowired
    private TestDao testDao;
 
    @RequestMapping("/test")
    public String getString() {
        int i = testDao.selectBase();
        return String.valueOf(i);
    }
}

springboot注解式AOP通過JoinPoint獲取參數(shù)

之前開發(fā)時(shí),需要獲取切點(diǎn)注解的參數(shù)值,記錄一下

切面注解 :

@Aspect – 標(biāo)識(shí)為一個(gè)切面供容器讀取,作用于類

@Pointcut – (切入點(diǎn)):就是帶有通知的連接點(diǎn)

@Before – 前置

@AfterThrowing – 異常拋出

@After – 后置

@AfterReturning – 后置增強(qiáng),執(zhí)行順序在@After之后

@Around – 環(huán)繞

1.相關(guān)maven包

 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-aop</artifactId>
 </dependency>

2.自定義一個(gè)接口

import java.lang.annotation.*;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Action {
    String value() default "list";
}

3.定義切面類

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@Aspect
@Component
public class ActAspect {
 
 @AfterReturning("@annotation(包名.Action)")
    public void afterReturning(JoinPoint point){
    
  // 獲取切入點(diǎn)方法名
  String methodName = point.getSignature().getName();
  
     // 獲取注解中的參數(shù)值
        MethodSignature methodSignature = (MethodSignature)point.getSignature();
        Method method = methodSignature.getMethod();
        // 獲取注解Action 
        Action annotation = method.getAnnotation(Action.class);
        // 獲取注解Action的value參數(shù)的值
        String value = annotation.value();
        
        // 獲取切點(diǎn)方法入?yún)⒘斜?
        Object[] objArray = point.getArgs();
        // 下面代碼根據(jù)具體入?yún)㈩愋瓦M(jìn)行修改
        List<String> list = new ArrayList<>();
        for (Object obj: objArray) {
            if(obj instanceof Collection){
                list = (List<String>) obj;
            }
        }
    }    
}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java?Web項(xiàng)目中如何添加Tomcat的Servlet-api.jar包(基于IDEA)

    Java?Web項(xiàng)目中如何添加Tomcat的Servlet-api.jar包(基于IDEA)

    servlet-api.jar是在編寫servlet必須用到的jar包下面這篇文章主要給大家介紹了基于IDEAJava?Web項(xiàng)目中如何添加Tomcat的Servlet-api.jar包的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2024-04-04
  • JPA merge聯(lián)合唯一索引無效問題解決方案

    JPA merge聯(lián)合唯一索引無效問題解決方案

    這篇文章主要介紹了JPA merge聯(lián)合唯一索引無效問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • 聊聊SpringBoot的@Scheduled的并發(fā)問題

    聊聊SpringBoot的@Scheduled的并發(fā)問題

    這篇文章主要介紹了聊聊SpringBoot的@Scheduled的并發(fā)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • idea mac版打開是出現(xiàn)打開失敗問題及解決

    idea mac版打開是出現(xiàn)打開失敗問題及解決

    這篇文章主要介紹了idea mac版打開是出現(xiàn)打開失敗問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • 使用@EnableWebMvc輕松配置Spring MVC

    使用@EnableWebMvc輕松配置Spring MVC

    這篇文章主要為大家介紹了使用@EnableWebMvc輕松配置Spring MVC實(shí)現(xiàn)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • java學(xué)習(xí)指南之字符串與正則表達(dá)式

    java學(xué)習(xí)指南之字符串與正則表達(dá)式

    在日常Java后端開發(fā)過程中,免不了對(duì)數(shù)據(jù)字段的解析,自然就少不了對(duì)字符串的操作,這其中就包含了正則表達(dá)式這一塊的內(nèi)容,下面這篇文章主要給大家介紹了關(guān)于java學(xué)習(xí)指南之字符串與正則表達(dá)式的相關(guān)資料,需要的朋友可以參考下
    2023-05-05
  • Java三目運(yùn)算符的實(shí)戰(zhàn)案例

    Java三目運(yùn)算符的實(shí)戰(zhàn)案例

    三目運(yùn)算符在java中運(yùn)用可以說非常的廣泛,下面這篇文章主要給大家介紹了關(guān)于Java三目運(yùn)算符的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-09-09
  • Spring Security自定義失敗處理器問題

    Spring Security自定義失敗處理器問題

    這篇文章主要介紹了Spring Security自定義失敗處理器問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Spring Boot與Kotlin 整合全文搜索引擎Elasticsearch的示例代碼

    Spring Boot與Kotlin 整合全文搜索引擎Elasticsearch的示例代碼

    本篇文章主要介紹了Spring Boot與Kotlin 整合全文搜索引擎Elasticsearch的示例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • idea查看properties中文變成unicode碼的解決方案

    idea查看properties中文變成unicode碼的解決方案

    這篇文章主要介紹了idea查看properties中文變成unicode碼的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06

最新評(píng)論