spring aop 无法将值传递给自定义参数
spring aop can't transfer value to customized parameter
这是一个spring引导应用程序,我使用aop来捕获http head,body.I可以在捕获http head的值时为@annotation做一些事情,body.However,当我传输时数据到我的自定义参数,我不能 get.but 另一个 spring 启动应用程序可以传输 it.I 不知道为什么。
这是定制的@Aspect:
@Component
@Aspect
public class ApiAspect {
@Resource
private HttpServletRequest request;
@Around("@annotation(com.jvbaoji.api.common.ApiAuth)")
public Object aroundAdvice(ProceedingJoinPoint point) throws Throwable {
Long userId = Optional.ofNullable(request.getHeader("user_id")).map(Long::valueOf).orElse(null);
final MethodSignature signature = (MethodSignature) point.getSignature();
final Method method = signature.getMethod();
final String methodName = method.getName();
final Class<?>[] parameterTypes = method.getParameterTypes();
final Annotation[][] parameterAnnotations;
parameterAnnotations = point.getTarget().getClass().getMethod(methodName, parameterTypes).getParameterAnnotations();
final Object[] args = point.getArgs();
for (int i = 0; i < args.length; i++) {
for (Annotation annotation : parameterAnnotations[i]) {
if (AuthUserId.class.equals(annotation.annotationType())) {
args[i] = userId;
break;
}
}
}
return point.proceed();
}
}
自定义@界面:
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiAuth {
}
参数:
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface AuthUserId {
}
控制器:
@RequestMapping(value = "updateAccount", method = RequestMethod.POST)
@ApiAuth
public JsonNode updateAccount(@AuthUserId Long userId, @ModelAttribute UserAccountVO userAccountVO) {
userAccountVO.setUserId(userId);
TransmissionResult result = userWalletService.updateAccount(userAccountVO);
if (!result.isSuccess()) {
return responseJsonFactory.createBad(result.getCode(), result.getFailReason());
}
return responseJsonFactory.createOk(jsonMapper.convertValue(userAccountVO, JsonNode.class));
}
我发现当我 post http 时,我可以调试到 arg[i]=userId
,但是到 return point.proceed()
,它会抛出一个 NullException.My @AuthUserId Long userId
无法获取 return value.I 不知道为什么。
ps:我的 spring aop 版本是 spring-aop-4.1.4.RELEASE.jar in spring boot.
感谢您提供的任何建议。
好吧,这是我的无知......我错过了添加参数以继续。
在ApiAspect.class中:
return point.proceed();
至
return point.proceed(args);
这是一个spring引导应用程序,我使用aop来捕获http head,body.I可以在捕获http head的值时为@annotation做一些事情,body.However,当我传输时数据到我的自定义参数,我不能 get.but 另一个 spring 启动应用程序可以传输 it.I 不知道为什么。
这是定制的@Aspect:
@Component
@Aspect
public class ApiAspect {
@Resource
private HttpServletRequest request;
@Around("@annotation(com.jvbaoji.api.common.ApiAuth)")
public Object aroundAdvice(ProceedingJoinPoint point) throws Throwable {
Long userId = Optional.ofNullable(request.getHeader("user_id")).map(Long::valueOf).orElse(null);
final MethodSignature signature = (MethodSignature) point.getSignature();
final Method method = signature.getMethod();
final String methodName = method.getName();
final Class<?>[] parameterTypes = method.getParameterTypes();
final Annotation[][] parameterAnnotations;
parameterAnnotations = point.getTarget().getClass().getMethod(methodName, parameterTypes).getParameterAnnotations();
final Object[] args = point.getArgs();
for (int i = 0; i < args.length; i++) {
for (Annotation annotation : parameterAnnotations[i]) {
if (AuthUserId.class.equals(annotation.annotationType())) {
args[i] = userId;
break;
}
}
}
return point.proceed();
}
}
自定义@界面:
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiAuth {
}
参数:
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface AuthUserId {
}
控制器:
@RequestMapping(value = "updateAccount", method = RequestMethod.POST)
@ApiAuth
public JsonNode updateAccount(@AuthUserId Long userId, @ModelAttribute UserAccountVO userAccountVO) {
userAccountVO.setUserId(userId);
TransmissionResult result = userWalletService.updateAccount(userAccountVO);
if (!result.isSuccess()) {
return responseJsonFactory.createBad(result.getCode(), result.getFailReason());
}
return responseJsonFactory.createOk(jsonMapper.convertValue(userAccountVO, JsonNode.class));
}
我发现当我 post http 时,我可以调试到 arg[i]=userId
,但是到 return point.proceed()
,它会抛出一个 NullException.My @AuthUserId Long userId
无法获取 return value.I 不知道为什么。
ps:我的 spring aop 版本是 spring-aop-4.1.4.RELEASE.jar in spring boot.
感谢您提供的任何建议。
好吧,这是我的无知......我错过了添加参数以继续。
在ApiAspect.class中:
return point.proceed();
至
return point.proceed(args);