用spring的websocket做在线聊天功能,遇到SimpMessagingTemplate 无法注入的问题

发布于 2022-05-05 12:21:35 字数 2229 浏览 951 评论 11

package main.java.com.fyd.websocket.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;


import main.java.com.fyd.websocket.entity.UserChatCommand;


@Controller 
public class CoordinationController {
//用于转发数据(sendTo)  
    private SimpMessagingTemplate template;
    
    @Autowired
    public CoordinationController(SimpMessagingTemplate t) {  
        this.template = t;  
    } 
    
    @MessageMapping("/userChat")  
    public void userChat(UserChatCommand userChat) {  
        //找到需要发送的地址  
        String dest = "/userChat/chat" + userChat.getCoordinationId();  
        //发送用户的聊天记录  
        this.template.convertAndSend(dest, userChat);  
    }  
}




以上是controller的代码
<bean id="coordinationController" class="main.java.com.fyd.websocket.controller.CoordinationController">
    </bean>


然后报这个错:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'coordinationController' defined in URL [file:/D:/MyDevPlace/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/mywebapp/WEB-INF/classes/spring-config.xml]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.messaging.simp.SimpMessagingTemplate]: : No qualifying bean of type [org.springframework.messaging.simp.SimpMessagingTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.messaging.simp.SimpMessagingTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}


以上是spring的xml 的bean配置


求助!!!!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

荒岛晴空╯ 2022-05-06 08:45:26

引用来自“雨翔河”的评论

为什么不使用 @SendTo 注解呢?

可以看

http://www.yuorfei.com/article?id=20

或者

http://my.oschina.net/110NotFound/blog/536975

自演自醉 2022-05-06 08:45:25

我都醉了,你仔细去看代码!

禾厶谷欠 2022-05-06 08:45:24

大师能不能具体指点一下呢

蹲在坟头点根烟丶 2022-05-06 08:45:06

大师,能把源码发我参考下么

山川志 2022-05-06 08:43:38

引用来自“雨翔河”的评论

为什么不使用 @SendTo 注解呢?

可以看

http://www.yuorfei.com/article?id=20

或者

http://my.oschina.net/110NotFound/blog/536975

你的笑 2022-05-06 08:41:57

试过了,也是一样报这个错

季末如歌 2022-05-06 08:41:55

取消构造方法(使用默认的无参数构造方法),再声明setter方法并配置spring。这样之后还会报同样的异常?

又怨 2022-05-06 08:35:19

tomcat8的,服务器肯定是不会错,controller注解我去掉了,也不好使,或者把bean配置去掉,一样。

情深缘浅√ 2022-05-06 08:13:06

这是不是环境问题?而且你的controller不是手动注入bean怎么还写
@controller

清风疏影 2022-05-06 07:39:54

嗯 好的 我研究下 谢谢!

最冷一天 2022-05-06 06:41:01

为什么不使用 @SendTo 注解呢?

可以看

http://www.yuorfei.com/article?id=20

或者

http://my.oschina.net/110NotFound/blog/536975

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文