Discussion on getSignature method of springboot and JoinPoint
- 2021-09-20 20:16:52
- OfStack
getSignature method of JoinPoint
When using springboot to write aop, there is an JoinPoint class, which is used to obtain the information of proxy class and proxy class.
This article records what format is returned by the getSignature method of JoinPoint under 1.
No nonsense, stick the code
package org.aspectj.lang;
public interface Signature {
String toString();
String toShortString();
String toLongString();
String getName();
int getModifiers();
Class getDeclaringType();
String getDeclaringTypeName();
}
Printout, getString is the method name of the test class, and TestController is the class name
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
The colon is preceded by the method used, followed by the output of this test.
Attach the class under test:
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 annotated AOP obtains parameters through JoinPoint
When developing before, you need to get the parameter value of tangent point annotation, and record it under 1
Section notes:
The @ Aspect identity is 1 section for the container to read and acts on the class
@ Pointcut (Entry Point): Is a connection point with notifications
@ Before front
@ AfterThrowing exception thrown
@ After post
Post enhancement of @ AfterReturning, followed by @ After
@ Around Wrap
1. Related maven package
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
2. Customize 1 interface
import java.lang.annotation.*;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Action {
String value() default "list";
}
3. Define the section class
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( Package name .Action)")
public void afterReturning(JoinPoint point){
// Get the pointcut method name
String methodName = point.getSignature().getName();
// Get parameter values in comments
MethodSignature methodSignature = (MethodSignature)point.getSignature();
Method method = methodSignature.getMethod();
// Get annotations Action
Action annotation = method.getAnnotation(Action.class);
// Get annotations Action Adj. value The value of the parameter
String value = annotation.value();
// Get the entry list of tangent point methods
Object[] objArray = point.getArgs();
// The following code is modified according to the specific input type
List<String> list = new ArrayList<>();
for (Object obj: objArray) {
if(obj instanceof Collection){
list = (List<String>) obj;
}
}
}
}