1、创建自定义参数注解
import org.springframework.core.annotation.AliasFor;
import java.lang.annotation.*;
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface User {
@AliasFor("name")
String value() default "";
@AliasFor("value")
String name() default "";
}
2、自定义参数注解的切面类
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
@Aspect
@Component
public class UserAdvice {
@Pointcut("execution(public * com.xxx.xxx.xxx.controller..*(..))")
@Order(2)
public void pointCut() {
}
@Around("pointCut()")
public Object run(ProceedingJoinPoint joinPoint) throws Throwable {
Object[] args = joinPoint.getArgs();
Signature signature1 = joinPoint.getSignature();
Method method1 = ((MethodSignature) signature1).getMethod();
Method realMethod = joinPoint.getTarget().getClass().getDeclaredMethod(signature1.getName(), method1.getParameterTypes());
String name = "";
int num = -1;
Annotation[][] parameterAnnotations = realMethod.getParameterAnnotations();
A:
for (Annotation[] annotations : parameterAnnotations) {
num = num + 1;
for (Annotation annotation : annotations) {
name = annotation.annotationType().getSimpleName();
if (name.equals("User")) {
break A;
}
}
}
if (name.equals("User")) {
args[num] = "1111";
}
return joinPoint.proceed(args);
}
}
3、使用方式
@GetMapping("/1111")
public void test111(@User String name) {
System.out.println(name);
}
4、调用