无法自动装配.找不到SimpMessagingTemplate类型的bean

Tk4*_*421 16 java stomp javabeans autowired spring-messaging

我基本上按照文档中提供的指南在Spring中配置Websockets.

我正在尝试从服务器向客户端发送消息,如 " 从任何地方发送消息 " 一节中所述

在示例之后,您可以自动装配名为SimpMessagingTemplate的类

@Controller
public class GreetingController {

    private SimpMessagingTemplate template;

    @Autowired
    public GreetingController(SimpMessagingTemplate template) {
        this.template = template;
    }

    @RequestMapping(value="/greetings", method=POST)
    public void greet(String greeting) {
        String text = "[" + getTimestamp() + "]:" + greeting;
        this.template.convertAndSend("/topic/greetings", text);
    }

}
Run Code Online (Sandbox Code Playgroud)

但是,我当前的项目找不到bean"SimpMessagingTemplate".(Intellij:'无法自动装配.没有找到SimpMessagingTemplate类型的bean'.

我在互联网上查了几个例子,但是我找不到如何让Spring创建一个SimpMessagingTemplate实例.我怎样才能自动装配它?

编辑:

我决定发送更多背景信息.这是我目前的websocket配置:

<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-4.0.xsd">

        <!-- TODO properties to be read from a properties file -->
        <websocket:message-broker application-destination-prefix="/app">
            <websocket:stomp-endpoint path="/new_session" >
                <websocket:sockjs/>
            </websocket:stomp-endpoint>
            <websocket:simple-broker prefix="/topic"/>
        </websocket:message-broker>
</beans>
Run Code Online (Sandbox Code Playgroud)

Websocket适用于此控制器

@Controller
public class SessionController {

    private static final Logger log = LoggerFactory.getLogger(SessionController.class);

    @MessageMapping("/new_session")
    @SendTo("/topic/session")
    public SessionStatus newSession(Session session) throws Exception {
    Thread.sleep(3000); // simulated delay
    log.info("Response sent !!");
    return new SessionStatus("StatusReport, " + session.toString() + "!");
    }
}
Run Code Online (Sandbox Code Playgroud)

我只是不确定如何使这项工作

public class SessionController {

    private static final Logger log = LoggerFactory.getLogger(SessionController.class);

    private SimpMessagingTemplate template;

    @Autowired
    public SessionController(SimpMessagingTemplate template) {
    this.template = template;
    }

}
Run Code Online (Sandbox Code Playgroud)

由于找不到bean"SimpMessagingTemplate模板".Spring文档没有提供有关此事的更多详细信息.

编辑:github中的工作代码示例

Urb*_*leg 9

我有同样的问题,因为我的websocket配置文件发生错误:

@Configuration
@EnableWebSocketMessageBroker
@EnableScheduling
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

}
Run Code Online (Sandbox Code Playgroud)

没有被春天扫描.

所以解决方法是将包含此配置文件的包添加到扫描的包中.


ear*_*ing 6

你应该在applicationContext xml中有一个与类名相同的bean定义id,或者在注入类时注释@Component以使Autowire工作

<bean id="SimpMessagingTemplate " class="your-class" >
Run Code Online (Sandbox Code Playgroud)

您可能需要定义下面标记指向您的包以供以后使用

<context:component-scan base-package="com.x.y"/>
Run Code Online (Sandbox Code Playgroud)

  • 请注意,SimpMessagingTemplate不是我的类是Spring Framework的一部分.该文档未提供有关如何获取该类实例的更多信息.如果我尝试在我的XML中添加bean,如<bean id ="simpMessagingTemplate"class ="org.springframework.messaging.simp.SimpMessagingTemplate"/>期望构造函数中的参数(MessageChannel接口),我不知道如何链接到当前的websocket配置. (4认同)

Ros*_*hev 5

奇怪,因为当你使用websocket命名空间时,"message-broker"元素会导致创建一个SimpMessagingTemplate bean,然后你可以使用它来注入.控制器和websocket命名空间是否在同一个ApplicationContext中,或者可能是"root"上下文中的一个,另一个是DispatcherServlet上下文中的?

  • 谢谢你的帮助。我发现了问题。我写了一个[代码证明](https://github.com/tk421/spring-stomp),然后我意识到问题是Intellij没有为SimpMessagingTemplate正确选择@Autowire。如果您现在运行该程序,它将正常工作。 (2认同)
  • @plkmthr问题是Intellij无法正确接收SimpMessagingTemplate。如果您运行它将起作用。检查指向“代码证明”的链接 (2认同)