首先引用我自己的(稍作修改的)评论,因为我仍然认为这两种方法是您应该首先尝试的:
您可能想了解一下
-
目标变量占位符 https://stackoverflow.com/a/27055764/1082681 for
@SendTo
or @SubscribeMapping
并且
- 我关于如何做的回答评估 SpEL(Spring 表达式语言) https://stackoverflow.com/a/53825701/1082681.
也许这两种方法中的一种对您来说是可行的。
话虽如此,您也可以转向原力的阴暗面并真正尝试操纵注释值。下面是 AspectJ 中的一些概念证明(不是 Spring AOP,但切入点语法是相同的):
示例驱动程序应用程序:
package de.scrum_master.app;
import org.springframework.messaging.handler.annotation.SendTo;
public class Application {
@SendTo("original")
public void doSomething() throws NoSuchMethodException, SecurityException {
SendTo sendTo = Application.class
.getDeclaredMethod("doSomething")
.getAnnotationsByType(SendTo.class)[0];
System.out.println(sendTo.value()[0]);
}
public static void main(String[] args) throws NoSuchMethodException, SecurityException {
new Application().doSomething();
new Application().doSomething();
new Application().doSomething();
}
}
如果没有方面,这将打印:
original
original
original
这里没有什么惊喜。现在使用这个方面(在Spring AOP中你还应该添加一个@Component
注解):
package de.scrum_master.aspect;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Proxy;
import java.util.Map;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.messaging.handler.annotation.SendTo;
@Aspect
public class SendToAspect {
@Before("execution(* *(..)) && @annotation(sendTo)")
public void changeAnnotation(JoinPoint thisJoinPoint, SendTo sendTo) {
System.out.println(thisJoinPoint + "\n [BEFORE] " + sendTo);
changeAnnotationValue(sendTo, "value", new String[] { "changed" });
System.out.println(" [AFTER] " + sendTo);
}
@SuppressWarnings("unchecked")
public static Object changeAnnotationValue(Annotation annotation, String key, Object newValue) {
Object handler = Proxy.getInvocationHandler(annotation);
Field f;
try {
f = handler.getClass().getDeclaredField("memberValues");
} catch (NoSuchFieldException | SecurityException e) {
throw new IllegalStateException(e);
}
f.setAccessible(true);
Map<String, Object> memberValues;
try {
memberValues = (Map<String, Object>) f.get(handler);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
Object oldValue = memberValues.get(key);
if (oldValue == null || oldValue.getClass() != newValue.getClass()) {
throw new IllegalArgumentException();
}
memberValues.put(key, newValue);
return oldValue;
}
}
现在控制台日志是:
execution(void de.scrum_master.app.Application.doSomething())
[BEFORE] @org.springframework.messaging.handler.annotation.SendTo(value=[original])
[AFTER] @org.springframework.messaging.handler.annotation.SendTo(value=[changed])
changed
execution(void de.scrum_master.app.Application.doSomething())
[BEFORE] @org.springframework.messaging.handler.annotation.SendTo(value=[changed])
[AFTER] @org.springframework.messaging.handler.annotation.SendTo(value=[changed])
changed
execution(void de.scrum_master.app.Application.doSomething())
[BEFORE] @org.springframework.messaging.handler.annotation.SendTo(value=[changed])
[AFTER] @org.springframework.messaging.handler.annotation.SendTo(value=[changed])
changed
正如您在上面的日志中看到的,如果您只需要更改该值一次并且其值不是动态的,那么每次调用该方法时都执行方面建议的效率很低。相反,您还可以从方面之外的其他位置操作注释,或者向方面添加静态布尔成员,以便仅操作注释一次:
static boolean done = false;
@Before("execution(* *(..)) && @annotation(sendTo)")
public void changeAnnotation(JoinPoint thisJoinPoint, SendTo sendTo) {
if (done)
return;
System.out.println(thisJoinPoint + "\n [BEFORE] " + sendTo);
changeAnnotationValue(sendTo, "value", new String[] { "changed" });
System.out.println(" [AFTER] " + sendTo);
done = true;
}
那么输出将是:
execution(void de.scrum_master.app.Application.doSomething())
[BEFORE] @org.springframework.messaging.handler.annotation.SendTo(value=[original])
[AFTER] @org.springframework.messaging.handler.annotation.SendTo(value=[changed])
changed
changed
changed
也可以看看:
-
Java-EX https://github.com/XDean/Java-EX项目,我上面方面的辅助方法取自there https://github.com/XDean/Java-EX/blob/master/src/main/java/xdean/jex/util/reflect/AnnotationUtil.java#L92
- Java-EX 的灵感还来自于所以问题 https://stackoverflow.com/a/28118436/1082681, 顺便一提。