- AOP以注解为切入点,获取注解参数和切点方法参数名
import cn.lettin.base.response.ResponseObjBaseVo; import cn.lettin.base.response.ResponseVo; import cn.lettin.keeper.edge.utils.UserNodeAuthCheckUtils; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; import java.util.Arrays; import java.util.List; import static cn.lettin.common.exception.GlobalException.OK; @Aspect @Component public class UserNodeAuthCheckAop { private final UserNodeAuthCheckUtils userNodeAuthCheckUtils; public UserNodeAuthCheckAop(UserNodeAuthCheckUtils userNodeAuthCheckUtils) { this.userNodeAuthCheckUtils = userNodeAuthCheckUtils; } @Around(value = "@annotation(cn.lettin.keeper.edge.external.aspect.UserNodeAuth)") public Object check(ProceedingJoinPoint point) throws Throwable { // 获取目标方法上的注解 MethodSignature methodSignature = (MethodSignature) point.getSignature(); UserNodeAuth userNodeAuth = methodSignature.getMethod().getAnnotation(UserNodeAuth.class); // 下面两个数组中,参数值和参数名的个数和位置是一一对应的。 Object[] args = point.getArgs(); // 参数值 List<String> argNames = Arrays.asList(methodSignature.getParameterNames()); // 参数名 String nodeId = (String) args[argNames.indexOf("nodeId")]; Integer userId = (Integer) args[argNames.indexOf("userId")]; Integer regionId = (Integer) args[argNames.indexOf("regionId")]; ResponseVo<ResponseObjBaseVo> responseVo = userNodeAuthCheckUtils.authCheck(nodeId, userId, regionId, userNodeAuth.value()); if (responseVo.getStatus() != OK) { return new ResponseVo<>(responseVo); } return point.proceed(); } }
转载自:https://blog.csdn.net/mengting2040/article/details/132433024