I'm working with Spring AOP and I'd like to be able to define a pointcut which is triggered whenever a method inside of a package, whose name is defined in a properties file, is called. That is, my pointcut would look something like
@Pointcut("within(${base.packageName}.*)")
public void MyPointCut() {}
and then if my config file had
base.packageName=foo.bar
then at runtime the pointcut would behave like this one
@Pointcut("within(foo.bar.*)")
public void MyPointCut() {}
I've tried several different things (e.g. using SpEL in the pointcut expression, configuring a class implementing the static pointcut interface) but nothing has worked.
Is there any way in spring to define a pointcut based on a value found in a configuration file?
In Spring AOP, a join point always represents a method execution. A pointcut is a predicate that matches the join points, and the pointcut expression language is a way of describing pointcuts programmatically.
JoinPoint: Joinpoint are points in your program execution where flow of execution got changed like Exception catching, Calling other method. PointCut: PointCut are basically those Joinpoints where you can put your advice(or call aspect). So basically PointCuts are the subset of JoinPoints.
Pointcut is an expression language of spring AOP which is basically used to match the target methods to apply the advice. It has two parts ,one is the method signature comprising of method name and parameters. Other one is the pointcut expression which determines exactly which method we are applying the advice to.
Explanation: Union means the methods that either pointcut matches. Intersection means the methods that both pointcuts match. Union is usually more useful. Explanation: Using the static methods in the org.
This is not possible as the annotation value must be a compile time constant expression. So your pointcut cannot resolve ${}
placeholder, as the placeholder resolution happens at runtime. See more here.
The fact that you cannot do this, may be by design.
I'm going to posit something to you here and I'd like you to think about the ramifications.
You are asking to be able to dynamically define a value to an Aspect Oriented construct. You are placing it in an externally accessible source that is un-validated. If a hostile, think in terms of security here, were to alter the point cut and execute some other piece of code (possibly even arbitrary) would you consider that safe?
AOP, while extremely valuable, puts most security researchers on edge.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With