自定义一个注解
//注解类
import java.lang.annotation.*;
@Target({ElementType.PARAMETER,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RecordUpdateNumberMethod {
String type() default "操作类型";
}
//实现类
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Map;
@Component
@Aspect
public class RecordUpdateNumberAop extends BaseClass {
@Pointcut("execution(* com.cn.controller..*.*(..))")
private void controllerAspect(){
}
@Before("controllerAspect()")
public void AfterReturning(JoinPoint pjp){
System.out.println("---------------------执行前通知----------------------");
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
Object target = pjp.getTarget();
System.out.println("拦截的实体类,就是当前正在执行的controller------"+target);
String methodName = pjp.getSignature().getName();
System.out.println("拦截的方法名称。当前正在执行的方法-------"+methodName);
Object[] args = pjp.getArgs();
System.out.println("-------拦截的方法参数--------");
for(int i=0;i<args.length;i++){
System.out.println("======"+args[i]);
}
System.out.println("-------拦截的方法参数--------");
Signature sig = pjp.getSignature();
System.out.println("拦截的参数类型---------"+sig);
MethodSignature msig = null;
if (!(sig instanceof MethodSignature)) {
throw new IllegalArgumentException("该注解只能用于方法");
}
msig = (MethodSignature) sig;
Class[] parameterTypes = msig.getMethod().getParameterTypes();
Object object = null;
Method method = null;
try {
method = target.getClass().getMethod(methodName, parameterTypes);
} catch (NoSuchMethodException e1) {
e1.printStackTrace();
} catch (SecurityException e1) {
e1.printStackTrace();
}
System.out.println("method----"+method);
if(null!=method){
if(method.isAnnotationPresent(RecordUpdateNumberMethod.class)){
RecordUpdateNumberMethod log = method.getAnnotation(RecordUpdateNumberMethod.class);
String type=log.type();
}
}
}
// 注解调用
..这是一个接口
@PostMapping(value = "")
@RecordUpdateNumberMethod(type="")
public Object addUploadFileManagement(){}