淺談springboot之JoinPoint的getSignature方法
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è)參考,也希望大家多多支持腳本之家。
- SpringBoot中使用?POI的示例代碼
- SpringBoot EasyPoi動(dòng)態(tài)導(dǎo)入導(dǎo)出的兩種方式實(shí)現(xiàn)方法詳解
- SpringBoot如何基于POI-tl和word模板導(dǎo)出龐大的Word文件
- SpringBoot集成POI實(shí)現(xiàn)Excel導(dǎo)入導(dǎo)出的示例詳解
- SpringBoot?AOP?@Pointcut切入點(diǎn)表達(dá)式排除某些類方式
- springboot?aop里的@Pointcut()的配置方式
- springboot中EasyPoi實(shí)現(xiàn)自動(dòng)新增序號(hào)的方法
- SpringBoot中使用JeecgBoot的Autopoi導(dǎo)出Excel的方法步驟
- springboot中poi使用操作方法
相關(guān)文章
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聊聊SpringBoot的@Scheduled的并發(fā)問題
這篇文章主要介紹了聊聊SpringBoot的@Scheduled的并發(fā)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11java學(xué)習(xí)指南之字符串與正則表達(dá)式
在日常Java后端開發(fā)過程中,免不了對(duì)數(shù)據(jù)字段的解析,自然就少不了對(duì)字符串的操作,這其中就包含了正則表達(dá)式這一塊的內(nèi)容,下面這篇文章主要給大家介紹了關(guān)于java學(xué)習(xí)指南之字符串與正則表達(dá)式的相關(guān)資料,需要的朋友可以參考下2023-05-05Java三目運(yùn)算符的實(shí)戰(zhàn)案例
三目運(yùn)算符在java中運(yùn)用可以說非常的廣泛,下面這篇文章主要給大家介紹了關(guān)于Java三目運(yùn)算符的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09Spring Boot與Kotlin 整合全文搜索引擎Elasticsearch的示例代碼
本篇文章主要介紹了Spring Boot與Kotlin 整合全文搜索引擎Elasticsearch的示例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01idea查看properties中文變成unicode碼的解決方案
這篇文章主要介紹了idea查看properties中文變成unicode碼的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06