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
I had the same problem, the error occurred because my websocket config file:
@Configuration
@EnableWebSocketMessageBroker
@EnableScheduling
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
}
was not scanned by spring.
so the fix was to add the package with this configuration file to the scanned packages.
Rossen was correct. The addition of the element will add the SimpMessagingTemplate bean to the context for injection. This has to be in the web app root context, not the context for Spring DispatchServlet. E.g., in the following web.xml file, the message-broker element should be included in app-root.xml file. Including only in app-mvc.xml will cause NoSuchBeanDefinitionException.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring/app-root.xml</param-value>
</context-param>
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring/app-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
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?
You shall either have a bean definition id with same name as class name in your applicationContext xml or annotate @Component on injecting class for Autowire to work
<bean id="SimpMessagingTemplate " class="your-class" >
You may need to define below tag pointing to your package for later case
<context:component-scan base-package="com.x.y"/>