淺談springboot之JoinPoint的getSignature方法
在使用springboot寫(xiě)aop的時(shí)候,有個(gè)JoinPoint類(lèi),用來(lái)獲取代理類(lèi)和被代理類(lèi)的信息。
這個(gè)文章記錄一下JoinPoint的getSignature方法返回的是什么格式。
不廢話(huà),貼代碼package org.aspectj.lang; public interface Signature { String toString(); String toShortString(); String toLongString(); String getName(); int getModifiers(); Class getDeclaringType(); String getDeclaringTypeName();}
打印輸出,getString是測(cè)試類(lèi)的方法名,TestController是類(lèi)名
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
冒號(hào)前面是使用的方法,后面是本次測(cè)試輸出的結(jié)果。
附上被測(cè)試的類(lèi):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通過(guò)JoinPoint獲取參數(shù)
之前開(kāi)發(fā)時(shí),需要獲取切點(diǎn)注解的參數(shù)值,記錄一下
切面注解 :@Aspect ? 標(biāo)識(shí)為一個(gè)切面供容器讀取,作用于類(lèi)
@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.定義切面類(lèi)
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){ // 獲取切入點(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)⒘斜鞳bject[] objArray = point.getArgs();// 下面代碼根據(jù)具體入?yún)㈩?lèi)型進(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è)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. CSS hack用法案例詳解2. input submit、button和回車(chē)鍵提交數(shù)據(jù)詳解3. 利用FastReport傳遞圖片參數(shù)在報(bào)表上展示簽名信息的實(shí)現(xiàn)方法4. 怎樣才能用js生成xmldom對(duì)象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?5. ASP.NET Core實(shí)現(xiàn)中間件的幾種方式6. 詳解盒子端CSS動(dòng)畫(huà)性能提升7. 使用HttpClient增刪改查ASP.NET Web API服務(wù)8. Jsp+Servlet實(shí)現(xiàn)文件上傳下載 刪除上傳文件(三)9. 詳解瀏覽器的緩存機(jī)制10. JSP servlet實(shí)現(xiàn)文件上傳下載和刪除
