无法自动装配。找不到SimpMessagingTemplate类型的bean [英] Could not autowire. No beans of SimpMessagingTemplate type found

查看:5099
本文介绍了无法自动装配。找不到SimpMessagingTemplate类型的bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am configuring Websockets in Spring basically by following the guide provided in the documentation.

我正在尝试从服务器向客户端发送消息,如解释在发送消息来自任何地方

I am currently trying to send a message from the server to the client as explained in the section "Sending messages from anywhere"

按照示例,您可以自动装配一个名为SimpMessagingTemplate的类

Following the example, you can Autowire a class called 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);
    }

}

然而,我当前的项目找不到豆SimpMessagingTemplate。 (Intellij:'无法自动装配。没有找到SimpMessagingTemplate类型的bean'。

However, my current project cannot find the bean "SimpMessagingTemplate". (Intellij: 'Could not autowire. No beans of SimpMessagingTemplate type found'.

我在互联网上查了几个例子,但我找不到如何让Spring创建一个SimpMessagingTemplate的实例。我如何自动装配?

I have check several examples in the internet but I cannot find how to get Spring to create an instance of SimpMessagingTemplate. How can I Autowire it ?

编辑:

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

I decided to send some more background information. This is my current websocket configuration:

<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>

Websocket适用于此控制器

Websocket works with this controller

@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() + "!");
    }
}

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

I just not sure how to to make this work

public class SessionController {

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

    private SimpMessagingTemplate template;

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

}

作为beanSimpMessagingTemplate模板找不到。 Spring文档没有提供有关此事的更多详细信息。

As the bean "SimpMessagingTemplate template" is not found. Spring documentation does not offer more details regarding this matter.

编辑 github

推荐答案

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

Strange, because when you use the websocket namespace, the "message-broker" element causes the creation of a SimpMessagingTemplate bean which should then be available for you to inject. Are both the controller and the websocket namespace in the same ApplicationContext or is perhaps one in the "root" context and the other in the DispatcherServlet context?

这篇关于无法自动装配。找不到SimpMessagingTemplate类型的bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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