淺談springboot之JoinPoint的getSignature方法
JoinPoint的getSignature方法
在使用springboot寫aop的時候,有個JoinPoint類,用來獲取代理類和被代理類的信息。
這個文章記錄一下JoinPoint的getSignature方法返回的是什么格式。
不廢話,貼代碼
package org.aspectj.lang; public interface Signature { String toString(); String toShortString(); String toLongString(); String getName(); int getModifiers(); Class getDeclaringType(); String getDeclaringTypeName(); }
打印輸出,getString是測試類的方法名,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
冒號前面是使用的方法,后面是本次測試輸出的結(jié)果。
附上被測試的類:
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ù)值,記錄一下
切面注解 :
@Aspect – 標識為一個切面供容器讀取,作用于類
@Pointcut – (切入點):就是帶有通知的連接點
@Before – 前置
@AfterThrowing – 異常拋出
@After – 后置
@AfterReturning – 后置增強,執(zhí)行順序在@After之后
@Around – 環(huán)繞
1.相關(guān)maven包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
2.自定義一個接口
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){ // 獲取切入點方法名 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(); // 獲取切點方法入?yún)⒘斜? Object[] objArray = point.getArgs(); // 下面代碼根據(jù)具體入?yún)㈩愋瓦M行修改 List<String> list = new ArrayList<>(); for (Object obj: objArray) { if(obj instanceof Collection){ list = (List<String>) obj; } } } }
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- SpringBoot中使用?POI的示例代碼
- SpringBoot EasyPoi動態(tài)導入導出的兩種方式實現(xiàn)方法詳解
- SpringBoot如何基于POI-tl和word模板導出龐大的Word文件
- SpringBoot集成POI實現(xiàn)Excel導入導出的示例詳解
- SpringBoot?AOP?@Pointcut切入點表達式排除某些類方式
- springboot?aop里的@Pointcut()的配置方式
- springboot中EasyPoi實現(xiàn)自動新增序號的方法
- SpringBoot中使用JeecgBoot的Autopoi導出Excel的方法步驟
- springboot中poi使用操作方法
相關(guān)文章
Java?Web項目中如何添加Tomcat的Servlet-api.jar包(基于IDEA)
servlet-api.jar是在編寫servlet必須用到的jar包下面這篇文章主要給大家介紹了基于IDEAJava?Web項目中如何添加Tomcat的Servlet-api.jar包的相關(guān)資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2024-04-04聊聊SpringBoot的@Scheduled的并發(fā)問題
這篇文章主要介紹了聊聊SpringBoot的@Scheduled的并發(fā)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11Spring Boot與Kotlin 整合全文搜索引擎Elasticsearch的示例代碼
本篇文章主要介紹了Spring Boot與Kotlin 整合全文搜索引擎Elasticsearch的示例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01idea查看properties中文變成unicode碼的解決方案
這篇文章主要介紹了idea查看properties中文變成unicode碼的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06