2

Is it possible to have a pointcut expression match based on the method argument name?

For example, I want to match all methods with employeeId as an argument.

public Employee findById(Integer employeeId);

I can't match by data type because it would be too broad.

I know I can use the wildcards to match anything i.e. "execution(* * (..))" and check for the argument name in the method body, but that seems excessive?

George
  • 2,820
  • 4
  • 29
  • 56
  • Sounds a bit odd, what would the aspect do? And can you give examples of methods you would like to not match? Maybe you could [annotate your parameters](https://stackoverflow.com/a/29714549/1225328) instead? – sp00m Nov 06 '19 at 18:04
  • I want the aspect to log something for every method that is dealing with `employeeId`. I don't want to match something like `addIntegers(Integer x, Integer y)`. I could use annotations, but then I would have to manually search through my app for these methods... – George Nov 06 '19 at 18:17
  • May be this post will help : https://stackoverflow.com/questions/29681675/how-to-write-an-aspect-pointcut-based-on-an-annotated-parameter – R.G Nov 07 '19 at 08:35

2 Answers2

1

NO. You can not match based on arg names; but it's possible to match based on the arg TYPEs. To solve your problem, you can use the nearest pointcut expression to catch the target methods, and then filtering these methods programmatically in your aspect.

  • 1
    The pointcut expression which catches all your desired methods in addition to minimum extra undesired methods. – Ahmadreza Sedighi Nov 06 '19 at 19:50
  • Let me add that matching argument names would also be a maintenance nightmare, even if it was possible. Argument names are often subject to refactoring (renaming), you could never rely on them. If you want to make them unique, wrap them into a specific class and match them in a type-safe way. AOP was never designed to replace clean code or refactoring, it is a means to complement it. So go refactor, then use AOP in a clean way. Your code will also get more readable and more maintainable. – kriegaex Nov 07 '19 at 16:27
0

Point Cut based on argument name is not supported in Spring AOP.

Supported Point Cut Designators are listed here: Supported Pointcut Designators

R.G
  • 6,436
  • 3
  • 19
  • 28