5
votes

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?

2
You can use XML aop configEvgeniy Dorofeev
I can't upvote your comment, but this worked perfectly. If you want to create a separate answer I'll accept it; if not then I'll post my own answer with basic code and credit you. Thanksrsmartin2011

2 Answers

3
votes

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.

1
votes

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.