java 无法自动导丝,未找到SimpMessagingTemplate类型的Bean

niknxzdl  于 2023-10-14  发布在  Java
关注(0)|答案(5)|浏览(264)

我在Spring中配置Websockets基本上是遵循文档中提供的指南。
我目前正试图从服务器发送一条消息到客户端,如“从任何地方发送消息”一节所述。
按照该示例,您可以自动连接名为SimpMessagingTemplate的类

  1. @Controller
  2. public class GreetingController {
  3. private SimpMessagingTemplate template;
  4. @Autowired
  5. public GreetingController(SimpMessagingTemplate template) {
  6. this.template = template;
  7. }
  8. @RequestMapping(value="/greetings", method=POST)
  9. public void greet(String greeting) {
  10. String text = "[" + getTimestamp() + "]:" + greeting;
  11. this.template.convertAndSend("/topic/greetings", text);
  12. }
  13. }

但是,我当前的项目找不到bean“SimpMessagingTemplate”。(Intellij:“无法自动连接。未找到SimpMessagingTemplate类型的Bean '。
我在网上查了几个例子,但我找不到如何让Spring创建SimpMessagingTemplate的示例。我如何自动连接它?

编辑:

我决定提供更多的背景信息。这是我当前的WebSocket配置:

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:websocket="http://www.springframework.org/schema/websocket"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/websocket
  8. http://www.springframework.org/schema/websocket/spring-websocket-4.0.xsd">
  9. <!-- TODO properties to be read from a properties file -->
  10. <websocket:message-broker application-destination-prefix="/app">
  11. <websocket:stomp-endpoint path="/new_session" >
  12. <websocket:sockjs/>
  13. </websocket:stomp-endpoint>
  14. <websocket:simple-broker prefix="/topic"/>
  15. </websocket:message-broker>
  16. </beans>

WebSocket与此控制器配合使用

  1. @Controller
  2. public class SessionController {
  3. private static final Logger log = LoggerFactory.getLogger(SessionController.class);
  4. @MessageMapping("/new_session")
  5. @SendTo("/topic/session")
  6. public SessionStatus newSession(Session session) throws Exception {
  7. Thread.sleep(3000); // simulated delay
  8. log.info("Response sent !!");
  9. return new SessionStatus("StatusReport, " + session.toString() + "!");
  10. }
  11. }

我只是不知道该怎么做

  1. public class SessionController {
  2. private static final Logger log = LoggerFactory.getLogger(SessionController.class);
  3. private SimpMessagingTemplate template;
  4. @Autowired
  5. public SessionController(SimpMessagingTemplate template) {
  6. this.template = template;
  7. }
  8. }

因为没有找到bean“SimpMessagingTemplate template”。Spring文档没有提供关于这个问题的更多细节。

EDITgithub工作代码示例

7vhp5slm

7vhp5slm1#

我也遇到了同样的问题,错误发生是因为我的WebSocket配置文件:

  1. @Configuration
  2. @EnableWebSocketMessageBroker
  3. @EnableScheduling
  4. public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
  5. }

没有被Spring扫描。
因此,修复方法是将具有此配置文件的包添加到扫描的包中。

guykilcj

guykilcj2#

奇怪的是,当您使用WebSocket命名空间时,“message-broker”元素会导致创建一个可用于注入的WebSocket MessagingTemplate bean。控制器和WebSocket命名空间是否都在同一个ApplicationContext中,或者可能一个在“根”上下文中,另一个在DispatcherServlet上下文中?

2vuwiymt

2vuwiymt3#

您应该在applicationContextxml中有一个与类名同名的bean定义id,或者在注入类时注解@Component以使Autowire工作

  1. <bean id="SimpMessagingTemplate " class="your-class" >

您可能需要在下面定义指向您的包的标记,以备以后使用

  1. <context:component-scan base-package="com.x.y"/>
vql8enpb

vql8enpb4#

罗森是对的。元素的添加将把SimpMessagingTemplate bean添加到注入的上下文中。这必须在web应用根上下文中,而不是Spring DispatchServlet的上下文。例如,在下面的web.xml文件中,message-broker元素应该包含在app-root.xml文件中。仅在app-mvc.xml中包含将导致NoSuchBeanDefinitionException。

  1. <context-param>
  2. <param-name>contextConfigLocation</param-name>
  3. <param-value>classpath:/spring/app-root.xml</param-value>
  4. </context-param>
  5. <servlet>
  6. <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
  7. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  8. <init-param>
  9. <param-name>contextConfigLocation</param-name>
  10. <param-value>classpath:/spring/app-mvc.xml</param-value>
  11. </init-param>
  12. <load-on-startup>1</load-on-startup>
  13. </servlet>
huus2vyu

huus2vyu5#

在我的例子中,我通过将Spring-Boot-Starter-Websocket的依赖项添加到pom.xml来修复错误。尽管我已经在类中导入了XML MessagingTemplate,但它仍然需要pom.xml中的依赖项:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-websocket</artifactId>
  4. </dependency>

相关问题