此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.2.0! |
发送消息
如果您想从
应用?任何应用程序组件都可以向brokerChannel
.
最简单的方法是注入SimpMessagingTemplate
和
使用它来发送消息。通常,您可以通过以下方式注入它
type,如下例所示:
@Controller
public class GreetingController {
private SimpMessagingTemplate template;
@Autowired
public GreetingController(SimpMessagingTemplate template) {
this.template = template;
}
@RequestMapping(path="/greetings", method=POST)
public void greet(String greeting) {
String text = "[" + getTimestamp() + "]:" + greeting;
this.template.convertAndSend("/topic/greetings", text);
}
}
但是,您也可以通过其名称 (brokerMessagingTemplate
),如果另一个
存在相同类型的 bean。