Java AOP JoinPoint不获取参数名称 [英] Java AOP JoinPoint does not get parameter names

查看:3243
本文介绍了Java AOP JoinPoint不获取参数名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用Java Spring Mvc和Spring AOP从用户那里查找参数名称。

我有一个控制器,它从用户那里获取参数并调用服务。

我有一个在服务之前运行的方面。

方面应检查username和apiKey参数是否存在。

这是我的代码:


I'm using Java Spring Mvc and Spring AOP to find the parameter names from the user.
I have a controller which get parameters from user and call a service.
I have an aspect that running before the service.
The aspect should check if username and apiKey parameters are exist.
Here is my code :

控制器:

Controller :

@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);
}

DomainService:

DomainService :

@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.**
}

In在这种情况下,我希望在我的方面得到域,用户和apiKey参数名称。

知道我在这里缺少什么吗?

谢谢,

或。

In this case, I'd expect to get on my aspect the "domain", "user" and "apiKey" parameter names.
Any idea what am i missing here ?
Thanks,
Or.

推荐答案

正如我在上面所说注释,取决于代理类型,您可以或不可以访问参数名称。如果你的bean实现了接口,那么JDK代理将由spring创建,在这种代理中,MethodSignature.getParameterNames()为null。如果您的bean没有实现接口,则创建CGLIB代理,其中MethodSignature.getParameterNames()被填充。

As I've said in above comment, depending on proxy type you can or can't have access to parameter names. If your bean implements interface, the JDK proxy will be created by spring, and in this kind of proxy MethodSignature.getParameterNames() is null. If your bean doesn't implement interface, CGLIB proxy is created, where MethodSignature.getParameterNames() is filled.

如果可以,您可以切换到CGLIB代理bean删除bean接口,它应该工作。

If you can, you may switch to CGLIB proxy bean by removing bean interfaces and it should work.

我现在正在努力解决这个问题,我无法删除接口。我想出了不同的解决方案。在界面上我可以通过一些自定义注释来标记我的参数:

I'm struggling with the same now, and I can't remove interfaces. I figured out different solution for this. On the interface I can mark my parameters by some custom annot:

interface MyInterface {
  void myMetod(@ParamName("foo") Object foo, @ParamName("bar") Object bar);
}

现在在AOP代理中我可以获得以下信息:

Now in AOP proxy I can get this information in:

MethodSignature.getMethod().getParameterAnnotations()

这篇关于Java AOP JoinPoint不获取参数名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆