对于最新的稳定版本,请使用 Spring Framework 6.2.10! |
发送消息
如果您想从 应用? 任何应用程序组件都可以将消息发送到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。