0

I am looking for a solution for the problem where I can configure the pointcut expressions dynamically by reading from a properties file or database.

for example: @Around("execution(* com.example.updateUser(..))")

in above example, we have hardcoded the expression. I am looking for the solution where I can read

execution(* com.example.updateUser(..)) and then use it in @Around annotation.

I did not come across similar problem on web. Any solution for such problem is highly appreciated. Thank you!!

Swapnil Hadge
  • 59
  • 2
  • 11
  • There isn;t. Why would you even want this? That could be quite dangerous to do in the first place. – M. Deinum Aug 03 '22 at 14:21
  • Why not follow an apporach, where you already have a point cut applied to all the methods by default and trigger you logic around the method call based on flag and otherwise just proceed with the join point ? – Sanyam Goel Aug 03 '22 at 14:49
  • Say I want to do some post/pre-processing for certain apis and this I want to be configurable. For instance, there are 5 APIs in my microservice and I want to call the post/pre-processing on any of the 5 based on some configurations. Now I want user to configure this as well. this could be easy if I have 5 MS but if the project is bigger, its difficult to hardcode it. Thats the reason I am trying to make it configurable. – Swapnil Hadge Aug 03 '22 at 14:55
  • If some new APIs are added then I should be able to apply the configuration on some of the new API as well. – Swapnil Hadge Aug 03 '22 at 14:57
  • @M.Deinum, "there isn't" seems to be a bit imprecise. Maybe you are interpreting the question differently, but as far as I understand it, my answer would address the OP's need in at least two different ways he can choose from. Given the fact that the question is super generic and could mean anything, I can understand that you said no, though. – kriegaex Aug 06 '22 at 08:00
  • 1
    Not with annotations you cannot. Unless you go the manual route (as you did in your answer). But with the annotation based approach that won't work (and generally is a bridge or 2 to far for most :) ). Also when doing "dynamic pointcuts" you probably want/need a couple of steps back and rethink the solution IMHO as that could be quite dangerous (to be able to modify that at runtime/externally). – M. Deinum Aug 08 '22 at 05:44
  • 1
    Well, the OP asked how to use dynamic pointcuts read from an external file, and I showed him two canonical and documented ways to do that in Spring. Of course, there is no way to use variables (non-constants) for annotation parameter values, because that is a JVM restriction and totally unrelated to Spring. Hence my two alternatives, which honestly I both do not find that complicated. Either you do need the flexibility and choose an alternative, or you do not and stick with hard-coded pointcuts in annotations which should cover 90% (but less than 100%) of all use cases. – kriegaex Aug 08 '22 at 07:05

1 Answers1

1

You can use schema-based AOP and define the pointcut in classical, old-school XML style. Spring XML config is a text file being read while starting up the application and thus would satisfy your requirement.

If you like to manually wire your pointcut into an aspect, you can do that, too. Whether you define a string variable or field containing the pointcut directly in your application or read the pointcut from a text file, is completely up to you. Search for the terms DefaultPointcutAdvisor and AspectJExpressionPointcut in my answer here, somewhere inside the "update 2" and "update 3" parapgraphs. There you will also find a link to a complete sample project.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • Though its not completely solved my problem, kriegaex actually suggested a way which could help to make AOP more configurable than using the annotation. Hence marking it as a answer. – Swapnil Hadge Aug 09 '22 at 06:21