SimpMessagingTemplate 没有合格的 bean 类型

Jes*_*ssy 2 spring spring-websocket

我正在尝试在我的项目中使用该类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: {}
Run Code Online (Sandbox Code Playgroud)

这是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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的 pom 中有这样的依赖:

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-messaging</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

出了什么问题,为什么SimpMessagingTemplatebean 不存在?

Ana*_*han 5

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

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

}
Run Code Online (Sandbox Code Playgroud)

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

@Autowired
public WebSocketUserNotificationManagerImpl(SimpMessagingTemplate template) {
    this.template = template;
}
Run Code Online (Sandbox Code Playgroud)