I think you should use ThreadLocal
to store parameters
. According to the documentation one request will be processed with one thread, so it should be safe. Documentation is here
How to implement thread-safety cache inside Aspect class in Java?
-
14-01-2022 - |
Domanda
I have an Aspect class (spring-aop). I have one advice that gets some parameters and stores it in class fields. Second advice uses these parameters. It is used in functional test, I need to make them thread-safe, because it can be run by several users simultaneously. How can I do it?
public class SomeAspect {
private String parameters;
public void saveParameters(JoinPoint jp) {
Object[] args = jp.getArgs();
Object firstArg = args[0];
this.parameters= (String) firstArg;
}
public void useParameters() {
doSmth(parameters);
}
}
Soluzione
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow