Spring 允许您在 @RequestMapping 中使用属性占位符,但不能在 @MessageMapping 中使用。这是因为MessageHandler。因此,我们需要覆盖默认的MessageHandler 来执行此操作。
WebSocketAnnotationMethodMessageHandler 不支持占位符,您需要自己添加此支持。
为简单起见,我刚刚在我的项目中创建了另一个 WebSocketAnnotationMethodMessageHandler 类,位于原始 org.springframework.web.socket.messaging 的同一包中,并使用相同的内容覆盖 SimpAnnotationMethodMessageHandler 中的 getMappingForMethod 方法,仅更改 SimpMessageMappingInfo 的构造方式将此方法与此方法一起使用(WebSocketAnnotationMethodMessageHandler 中的private):
private SimpMessageMappingInfo createMessageMappingCondition(final MessageMapping annotation) {
return new SimpMessageMappingInfo(SimpMessageTypeMessageCondition.MESSAGE, new DestinationPatternsMessageCondition(
this.resolveAnnotationValues(annotation.value()), this.getPathMatcher()));
}
private SimpMessageMappingInfo createSubscribeCondition(final SubscribeMapping annotation) {
final SimpMessageTypeMessageCondition messageTypeMessageCondition = SimpMessageTypeMessageCondition.SUBSCRIBE;
return new SimpMessageMappingInfo(messageTypeMessageCondition, new DestinationPatternsMessageCondition(
this.resolveAnnotationValues(annotation.value()), this.getPathMatcher()));
}
这些方法现在将解析值考虑属性(调用resolveAnnotationValues 方法),所以我们需要使用这样的东西:
private String[] resolveAnnotationValues(final String[] destinationNames) {
final int length = destinationNames.length;
final String[] result = new String[length];
for (int i = 0; i < length; i++) {
result[i] = this.resolveAnnotationValue(destinationNames[i]);
}
return result;
}
private String resolveAnnotationValue(final String name) {
if (!(this.getApplicationContext() instanceof ConfigurableApplicationContext)) {
return name;
}
final ConfigurableApplicationContext applicationContext = (ConfigurableApplicationContext) this.getApplicationContext();
final ConfigurableBeanFactory configurableBeanFactory = applicationContext.getBeanFactory();
final String placeholdersResolved = configurableBeanFactory.resolveEmbeddedValue(name);
final BeanExpressionResolver exprResolver = configurableBeanFactory.getBeanExpressionResolver();
if (exprResolver == null) {
return name;
}
final Object result = exprResolver.evaluate(placeholdersResolved, new BeanExpressionContext(configurableBeanFactory, null));
return result != null ? result.toString() : name;
}
你仍然需要在你的配置中定义一个PropertySourcesPlaceholderConfigurer bean。
如果您使用基于 XML 的配置,请包括以下内容:
<context:property-placeholder location="classpath:/META-INF/spring/url-mapping-config.properties" />
如果你使用的是基于Java的配置,你可以这样尝试:
@Configuration
@PropertySources(value = @PropertySource("classpath:/META-INF/spring/url-mapping-config.properties"))
public class URLMappingConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
Obs.:在这种情况下,url-mapping-config.properties 文件位于 src\main\resources\META-INF\spring 文件夹中的 gradle/maven 项目中,内容如下所示:
myPropertyWS=urlvaluews
这是我的示例控制器:
@Controller
public class WebSocketController {
@SendTo("/topic/test")
@MessageMapping("${myPropertyWS}")
public String test() throws Exception {
Thread.sleep(4000); // simulated delay
return "OK";
}
}
使用默认的MessageHandler 启动日志将打印如下内容:
INFO: Mapped "{[/${myPropertyWS}],messageType=[MESSAGE]}" onto public java.lang.String com.brunocesar.controller.WebSocketController.test() throws java.lang.Exception
现在用我们的MessageHandler 打印这个:
INFO: Mapped "{[/urlvaluews],messageType=[MESSAGE]}" onto public java.lang.String com.brunocesar.controller.WebSocketController.test() throws java.lang.Exception
在这个gist 中查看完整的WebSocketAnnotationMethodMessageHandler 实现。
编辑:此解决方案解决了4.2 GA 之前版本的问题。如需更多信息,请参阅thisjira。