发送消息

如果您想从 应用?任何应用程序组件都可以向brokerChannel. 最简单的方法是注入SimpMessagingTemplate和 使用它来发送消息。通常,您可以通过以下方式注入它 type,如下例所示: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