SimpMessagingTemplate 没有类型的限定 bean

SimpMessagingTemplate no qualifying bean of type

我正在尝试在我的项目中使用 SimpMessagingTemplate class 通过 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 不存在?

好的,所以在您的应用程序上下文中找不到 bean SimpMessagingTemplate。因此请确保该 bean 在启动时由 spring 在应用程序上下文中自动创建,并且注释提供为像这样的创作所必需的:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

}

尝试像这样使用构造函数注入:

@Autowired
public WebSocketUserNotificationManagerImpl(SimpMessagingTemplate template) {
    this.template = template;
}
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer 

已弃用。

可以添加依赖-

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

然后实现WebSocketMessageBrokerConfigurer