此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Framework 6.2.10spring-doc.cadn.net.cn

发送消息

如果您想从 应用?任何应用程序组件都可以将消息发送到brokerChannel. 最简单的方法是注入一个SimpMessagingTemplate和 用它来发送消息。通常,您可以通过以下方式注入它 类型,如以下示例所示:spring-doc.cadn.net.cn

@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。spring-doc.cadn.net.cn