Spring WebSockets XML 配置不提供 brokerMessagingTemplate [英] Spring WebSockets XML configuration not providing brokerMessagingTemplate

查看:48
本文介绍了Spring WebSockets XML 配置不提供 brokerMessagingTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将使用 STOMP 的 WebSockets 支持添加到使用 XML 配置的 Spring MVC 应用程序.到目前为止,这一切进展顺利,我已经设法让 WebSockets 服务器侦听,并且 stomp.js 可以连接到它并发送消息和接收响应.

I'm trying to add in WebSockets support using STOMP to a Spring MVC application that is configured using XML. So far this has gone really well, and I've managed to get a WebSockets server listening, and stomp.js can connect to it and send messages and receive responses.

我还没有设法开始工作的是支持服务器向客户端发送任意消息,这些消息不是对从客户端收到的响应的响应.这意味着到目前为止,这实际上只是一个更复杂的 REST 版本,并没有太大用处.

What I've not managed to get working yet is the support for the server to send arbitrary messages to the client that aren't responses to one received from the client. That means this is actually just a more complicated version of REST so far, which isn't too useful.

我的 XML 配置如下所示:

My XML config looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:websocket="http://www.springframework.org/schema/websocket"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket.xsd">

  <websocket:message-broker>
    <websocket:stomp-endpoint path="/api/websocket/stomp" allowed-origins="*">
    </websocket:stomp-endpoint>

    <websocket:simple-broker prefix="/topic,/queue" />

    <websocket:message-converters>
        <bean class="org.springframework.messaging.converter.MappingJackson2MessageConverter">
            <property name="objectMapper" ref="objectMapper" />
        </bean>
    </websocket:message-converters>
   </websocket:message-broker>

  <bean class="uk.co.grahamcox.webapp.DebugController">
    <constructor-arg name="clock" ref="clock" />
    <constructor-arg name="template" ref="brokerMessagingTemplate" />
  </bean>
</beans>

(DebugController 是一个具有返回服务器时间的单一方法的类,作为 REST 和 WS 处理程序都可以正常工作)

(DebugController is a class that has a single method to return the server time, working fine as both a REST and a WS handler)

在启动时我得到:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'brokerMessagingTemplate' is defined

令人沮丧的是,IntelliJ 为我自动完成了brokerMessagingTemplate"引用,我可以点击它进入 AbstractMessageBrokerConfiguration 中的 @Bean 定义.

The frustrating thing is that IntelliJ auto-completed the "brokerMessagingTemplate" reference for me, and I can click through on it to an @Bean definition in AbstractMessageBrokerConfiguration.

我假设我在 XML 中缺少一些配置来完成这项工作,但我无法在文档中找到这会是什么.

I'm assuming that I'm missing some bit of config in the XML to make this work, but I can't find out in the docs what this would be.

有什么建议吗?

推荐答案

支持服务器向客户端发送任意消息不是对从客户端收到的响应.

support for the server to send arbitrary messages to the client that aren't responses to one received from the client.

向客户端发送消息的一种方法是让他们先订阅一个主题——确保理解应用程序目的地前缀"和代理前缀"之间的区别.在这种特殊情况下,您希望客户端订阅代理目标,然后您的服务器可以随时向所有这些客户端发送消息.

One way to send messages to clients is for them to first subscribe to a topic — make sure to understand the difference between "application destination prefixes" and "broker prefixes". In this particular case, you want clients to subscribe to a broker destination and then your server can send messages to all those clients, at any time.

理解这一点的最好方法是查看参考文档中的消息流.

The best way to understand this is to check out the flow of messages in the reference documentation.

要发送这些消息,您的应用程序代码需要一个消息模板.

To send those messages, your application code needs a messaging template.

您可以通过将表单 bean 名称切换为实际 bean 类型 SimpMessagingTemplate 来修复您的代码示例.

You can fix your code example by switching form bean name to the actual bean type SimpMessagingTemplate.

  <bean class="uk.co.grahamcox.webapp.DebugController">
    <constructor-arg name="clock" ref="clock" />
    <constructor-arg name="template" class="org.springframework.messaging.simp.SimpMessagingTemplate" />
  </bean>

参考文档提到了那个 bean 名称,但是在使用 XML 配置时它似乎没有用这个名称注册.随意创建一个 JIRA 问题来改进这一点.

The reference documentation mentions that bean name but it seems that it's not registered with this name when using the XML configuration. Feel free to create a JIRA issue to improve this.

这篇关于Spring WebSockets XML 配置不提供 brokerMessagingTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆