Spring AOP: get access to argument names

后端 未结 4 1236
小鲜肉
小鲜肉 2020-12-16 05:26

I\'m using Spring 3.x, Java 6.

I have an @Around aspect with the following joinpoint:

@Around(\"execution(public * my.service.*.*Connector.*(..))\")
         


        
4条回答
  •  再見小時候
    2020-12-16 05:36

    Check the implementations of org.springframework.core.ParameterNameDiscoverer.

    Annotations like @RequestParam used by spring inspect the parameter name if no value is set. So @RequestParam String foo will in fact fetch the request parameter named "foo". It uses the ParameterNameDiscoverer mechanism. I'm just not sure which of the implementations are used, by try each of them.

    The LocalVariableTableParameterNameDiscoverer reads the .class and uses asm to inspect the names.

    So, it is possible. But make sure to cache this information (for example - store a parameter name in a map, with key = class+method+parameter index).

    But, as it is noted in the docs, you need the debug information. From the docs of @PathVariable:

    The matching of method parameter names to URI Template variable names can only be done if your code is compiled with debugging enabled. If you do not have debugging enabled, you must specify the name of the URI Template variable name in the @PathVariable annotation in order to bind the resolved value of the variable name to a method parameter

    So, if you really don't want to include that information, Tomasz Nurkiewicz's answer explains the workaround.

提交回复
热议问题