Skip to content
Advertisement

How to write an Aspect pointcut based on an annotated parameter

I’m having a bit of trouble working out how to create a pointcut that will operate on beans that have a specific annotated parameter. My eventual aim is to validate the value of the parameter before it’s processed, but for the moment I just need to create the pointcut.

Consider the following annotation

JavaScript

I’d then like to apply this to a number of methods like:

JavaScript

So

  • I don’t care which class (or package) the methods are in
  • The position of the annotated argument will vary.
  • I do know that annotated value will only apply to a specific type

My pointcut implementation needs to be something along the lines of:

JavaScript

I’m getting a bit confused about exactly what that @Before value needs to be and how to tie in the annotation and its type. At this point it’s probably not worth listing the things I’ve tried!

Update: Based on the advice I’ve seen in http://stackoverflow.com/questions/3565718/pointcut-matching-methods-with-annotated-parameters/3567170#3567170 (and correcting a couple of misunderstandings and adding space I overlooked) I’ve got to the point where the following works:

JavaScript

This is almost what I need – all I need to do is pass the value of the annotated argument as an parameter to the method. I can’t quite work out the syntax to get Spring to do this (the linked answer does not show this).

Advertisement

Answer

Very similar to my answer here which sheltem already pointed to, the solution looks like this (in annotation-style syntax this time because in Spring AOP you cannot use native AspectJ syntax):

Original poster’s annotation:

JavaScript

Driver Application:

I use the driver application in order to test my AspectJ solution. In Spring the class as well as the aspect need to be Spring beans/components in order for this to work.

JavaScript

Aspect:

JavaScript

Console log:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement