您的位置:首页技术文章
文章详情页

浅谈springboot之JoinPoint的getSignature方法

浏览:3日期:2023-03-03 18:29:35
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; }} } }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持好吧啦网。

标签: Spring
相关文章: