Could not autowire. No beans of SimpMessagingTempl

2019-02-06 06:04发布

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"

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);
    }

}

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

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 ?

EDIT:

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

}

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

EDIT: Example of working code in github

4条回答
The star\"
2楼-- · 2019-02-06 06:20

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>
查看更多
贼婆χ
3楼-- · 2019-02-06 06:24

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?

查看更多
\"骚年 ilove
4楼-- · 2019-02-06 06:29

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"/>
查看更多
一纸荒年 Trace。
5楼-- · 2019-02-06 06:31

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.

查看更多
登录 后发表回答