【问题标题】:SimpMessagingTemplate no qualifying bean of typeSimpMessagingTemplate 没有符合条件的 bean 类型
【发布时间】:2020-07-22 08:54:57
【问题描述】:

我正在尝试在我的项目中使用 SimpMessagingTemplate 类通过 websocket 发送一些数据。 问题是我总是出错,这里是完整的错误:

Error creating bean with name 'webSocketUserNotificationManagerImpl' defined in URL [...]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.messaging.simp.SimpMessagingTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

这里是webSocketuserNotificationManagerImpl 服务:

@Service
public class WebSocketUserNotificationManagerImpl implements UserNotificationManager {

    /**
     * Template to use to send back a message to the user via Web-socket.
     */
    private SimpMessagingTemplate template;

    public WebSocketUserNotificationManagerImpl(SimpMessagingTemplate template) {
        this.template = template;
    }

    private ObjectMapper jsonMapper = new ObjectMapper();

    @Override
    public void sendMessageToUser(String jobName, String eventType, Result result) {
       ....
    }

    public void sendDeleteMessageToUser(DeleteRequestsSocketMessage message, Principal principal) {
        this.template.convertAndSendToUser(principal.getName().toLowerCase(),
                                           "/status/delete", message);
    }
}

我的 pom 中有这个依赖项:

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-messaging</artifactId>
</dependency>

怎么了,为什么SimpMessagingTemplate bean 不存在?

【问题讨论】:

    标签: spring spring-websocket


    【解决方案1】:

    好的,所以在您的应用程序上下文中找不到 bean SimpMessagingTemplate。所以请确保 bean 在启动时由 spring 在应用程序上下文中自动创建,并根据需要为创建提供注释,例如:

    @Configuration
    @EnableWebSocketMessageBroker
    public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
    
    }
    

    尝试使用构造函数注入,例如:

    @Autowired
    public WebSocketUserNotificationManagerImpl(SimpMessagingTemplate template) {
        this.template = template;
    }
    

    【讨论】:

      【解决方案2】:
      public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer 
      

      已被弃用。

      你可以添加依赖——

      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-websocket</artifactId>
      </dependency>
      

      然后实现WebSocketMessageBrokerConfigurer

      【讨论】:

        猜你喜欢
        • 2021-01-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-15
        • 2018-12-20
        • 2023-03-26
        相关资源
        最近更新 更多