淺談springboot之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():getStringjoinPoint.getSignature().getModifiers():1joinPoint.getSignature().getDeclaringType():class com.fast.web.controller.TestControllerjoinPoint.getSignature().getDeclaringTypeName():com.fast.web.controller.TestController
冒號前面是使用的方法,后面是本次測試輸出的結果。
附上被測試的類: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; @RestControllerpublic class TestController { @Autowired private TestDao testDao; @RequestMapping('/test') public String getString() {int i = testDao.selectBase();return String.valueOf(i); }}springboot注解式AOP通過JoinPoint獲取參數
之前開發時,需要獲取切點注解的參數值,記錄一下
切面注解 :@Aspect ? 標識為一個切面供容器讀取,作用于類
@Pointcut ? (切入點):就是帶有通知的連接點
@Before ? 前置
@AfterThrowing ? 異常拋出
@After ? 后置
@AfterReturning ? 后置增強,執行順序在@After之后
@Around ? 環繞
1.相關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@Componentpublic class ActAspect { @AfterReturning('@annotation(包名.Action)') public void afterReturning(JoinPoint point){ // 獲取切入點方法名 String methodName = point.getSignature().getName(); // 獲取注解中的參數值MethodSignature methodSignature = (MethodSignature)point.getSignature();Method method = methodSignature.getMethod();// 獲取注解Action Action annotation = method.getAnnotation(Action.class);// 獲取注解Action的value參數的值String value = annotation.value();// 獲取切點方法入參列表Object[] objArray = point.getArgs();// 下面代碼根據具體入參類型進行修改List<String> list = new ArrayList<>();for (Object obj: objArray) { if(obj instanceof Collection){list = (List<String>) obj; }} } }
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持好吧啦網。
相關文章:
1. phpstudy apache開啟ssi使用詳解2. CentOS郵件服務器搭建系列—— POP / IMAP 服務器的構建( Dovecot )3. .NET SkiaSharp 生成二維碼驗證碼及指定區域截取方法實現4. IntelliJ IDEA創建web項目的方法5. 存儲于xml中需要的HTML轉義代碼6. docker容器調用yum報錯的解決辦法7. django創建css文件夾的具體方法8. MyBatis JdbcType 與Oracle、MySql數據類型對應關系說明9. ASP中實現字符部位類似.NET里String對象的PadLeft和PadRight函數10. javascript xml xsl取值及數據修改第1/2頁
