Could not autowire. No beans of SimpMessagingTemplate type found

后端 未结 4 940
自闭症患者
自闭症患者 2020-12-31 03:09

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

相关标签:
4条回答
  • 2020-12-31 03:25

    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.

    0 讨论(0)
  • 2020-12-31 03:31

    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>
    
    0 讨论(0)
  • 2020-12-31 03:33

    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?

    0 讨论(0)
  • 2020-12-31 03:33

    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"/>
    
    0 讨论(0)
提交回复
热议问题