【发布时间】:2014-10-03 06:54:17
【问题描述】:
我正在使用 Java Spring Mvc 和 Spring AOP 从用户那里查找参数名称。
我有一个控制器,它从用户那里获取参数并调用服务。
我有一个在服务之前运行的方面。
方面应检查用户名和 apiKey 参数是否存在。
这是我的代码:
控制器:
@RequestMapping(method = RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody String getDomainWithFoundIn(@RequestParam (value="domain") String domain, @RequestParam (value="user") String user, @RequestParam (value="apiKey") String apiKey) throws JsonGenerationException, JsonMappingException, IOException {
return domainService.getDomainDataWithFoundIn(domain, user, apiKey);
}
域服务接口:
public interface IDomainService {
public String getDomainDataWithFoundIn(String domainStr, String user, String apiKey);
}
域服务:
@Override
@ApiAuthentication
public String getDomainDataWithFoundIn(String domainStr, String user, String apiKey) {
//Do stuff here
}
还有我的 AOP 类:
@Component
@Aspect
public class AuthAspect {
@Before("@annotation(apiAuthentication)")
public void printIt (JoinPoint joinPoint, ApiAuthentication apiAuthentication) throws NoAuthenticationParametersException, UserWasNotFoundException {
final MethodSignature signature = (MethodSignature) joinPoint.getSignature();
final String[] parameterNames = signature.getParameterNames();
**//parameterNames is null here.**
}
在这种情况下,我希望获得“域”、“用户”和“apiKey”参数名称。
知道我在这里缺少什么吗?
谢谢,
或者。
【问题讨论】:
-
你已经有点说了你所期望的。现在告诉我们实际发生了什么。
-
我在方面类上写的 - 我在 String[] parameterNames 上得到了 null 而不是参数名称。
-
你是如何编译你的源代码的?
-
我会先仔细检查一下。然后尝试调试并查看它去哪里找到参数名称。在 Spring 3.x 中,它会检查字节码,但可能在 Spring 4 中它使用新的
Method方法来获取Parameter对象。 -
这种情况发生在 AOP 与接口相交时,如果是类参数名称被填充。这可能是因为 spring 使用了不同的接口和类代理(CGLIB vs JDK)docs.spring.io/spring/docs/current/spring-framework-reference/…
标签: java spring spring-mvc spring-aop spring-aspects