Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring AOP - Pointcut Based on Value from Properties File

Tags:

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?

like image 837
rsmartin2011 Avatar asked Dec 10 '17 04:12

rsmartin2011


People also ask

How do I declare a pointcut in Spring AOP?

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.

What is the difference between JoinPoint and pointcut?

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.

What methods does the pointcut expression reference?

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.

Which of the following option will match the given pointcut execution?

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.


2 Answers

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.

like image 83
HelloWorld Avatar answered Oct 15 '22 11:10

HelloWorld


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.

like image 40
Dave G Avatar answered Oct 15 '22 09:10

Dave G