1 回答
TA贡献1824条经验 获得超6个赞
您可以创建一个保险箱ThreadLocal并设置所需的变量,然后使用它。
public class VariableContext {
private static ThreadLocal<String> currentVariable = new ThreadLocal<String>() {
@Override
protected String initialValue() {
return "";
}
};
public static void setCurrentVariable(String tenant) {
currentVariable.set(tenant);
}
public static String getCurrentVariable() {
return currentVariable.get();
}
public static void clear() {
currentVariable.remove();
}
}
您可以在这里或其他课程中使用它。
@Aspect
@EnableAspectJAutoProxy
public class SomeConfig {
@Around(value = "@annotation(path.to.my.annotation.here)", argNames = "specificArg")
public void doLogic(ProceedingJoinPoint joinPoint) throws Throwable {
//create local variable X for thread execution here
try{
joinPoint.proceed(); //or joinPoint.proceed(Object[]);
}
finally {
//use local variable X to do some logic
VariableContext.setCurrentVariable("someValue");
String result = VariableContext.getCurrentVariable();
}
}
}
添加回答
举报