Reputation: 936
I would like to annotate some methods and then deeper inside the execution chain be able to check if any method along the way was marked with an annotation?
Not sure if annotations are capable of that? I'm using Spring so maybe AOP
could come in help or maybe AST
transformations(I'm using groovy). What's easiest...
My use case would be to mark some methods with a custom annotation e.g. @UseReplicaIfAvailable
and then in service layer when I do a sql call I could decide If I can use a replica db(I don't care then about the freshness of data) or not...
I just want to avoid passing some kind of boolean
value through every method invocation.
Upvotes: 0
Views: 55
Reputation: 2366
Maybe you can do something like in this way:
@Aspect
@Component
public class EmployeeAspect {
private bolean wasExecuted = false;
@Before("@annotation(com.annotation.YourAnnotanion)") // or @After
public void secExecution(){
this.wasExecuted = true;
}
//getter, setter
}
This is what came to my mind, additionaly you have to reset back flag to false value after execution chain finish (you can do it maybe by chainging this bean scope etc). Good luck.
Upvotes: 1