在WebSocket往客户端推送消息的开发过程中,获取推送消息实例SimpMessagingTemplate
时,错误如下:
1 | [ERROR]-[2016年-10月-19日17:53:25.026]-[RMI TCP Connection(2)-127.0.0.1]-[org.springframework.web.context.ContextLoader]-{Context initialization failed} org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.zw.socket.service.controller.InstanceMessageController#0' defined in class path resource [spring-config/spring-socket-servlet.xml]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.messaging.simp.SimpMessagingTemplate] is defined: expected single matching bean but found 2:org.springframework.messaging.simp.SimpMessagingTemplate#0,brokerMessagingTemplate |
发生此错误的原因是在配置文件spring-socket-servlet.xml
里重复做了如下配置:
1 | <websocket:message-broker application-destination-prefix="/app"> |
扫描配置文件时会自动生成一个brokerMessagingTemplate
实例,与实例simpMessagingTemplate
冲突。解决的方法就是去掉配置文件中的配置。
参考:
[Could not autowire. No beans of SimpMessagingTemplate type found]
[Could not autowire. No beans of SimpMessagingTemplate type found]:http://stackoverflow.com/questions/22925951/could-not-autowire-no-beans-of-simpmessagingtemplate-type-found