如何更改运行时中的方法注释值?

2024-05-20

我有像这样的控制器

@MessageMapping("/room.register")
@SendTo("#{sendTo}")
public Message addUser(@Payload Message message,
                       SimpMessageHeaderAccessor headerAccessor) {
    headerAccessor.getSessionAttributes().put("username", 
    message.getSender());
    return message;

}

我想在运行时更改 SendTo 注释的值。

我尝试按如下方式进行:

@Aspect
public class SendToAspect {
@Autowired
private WebSocketConfigurationProperties webSocketConfigurationProperties;


@Around("execution (public * *(..)) && @annotation(ann)")
public Object execute(final ProceedingJoinPoint point, final SendTo ann) 
throws Throwable {

    MethodSignature signature = (MethodSignature) point.getSignature();
    Method method = signature.getMethod();
    method.setAccessible(true);

    Annotation[] annotations = method.getDeclaredAnnotations();
    for (int i = 0; i < annotations.length; i++) {
        if (annotations[i].annotationType().equals(SendTo.class)) {
            annotations[i] = new SendTo() {

                @Override
                public Class<? extends Annotation> annotationType() {
                    return SendTo.class;
                }

                @Override
                public String[] value() {
                    return new String[] 
                            {webSocketConfigurationProperties.getTopic()};
                }
            };
        }
    }
    return point.proceed();
}

}

但这仅在注释数组(Annotation[]注释)中发生变化,而在方法注释(method.getDeclaredAnnotations())中不会发生变化。

请告诉我该怎么做,这可能吗?


首先引用我自己的(稍作修改的)评论,因为我仍然认为这两种方法是您应该首先尝试的:

您可能想了解一下

  • 目标变量占位符 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, 顺便一提。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何更改运行时中的方法注释值? 的相关文章

随机推荐