I'm trying to use the SimpMessagingTemplate
class in my project to send some data via websocket.
The problem is that I always have an error, here is the complete error:
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: {}
And here is the webSocketuserNotificationManagerImpl
service:
@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);
}
}
I have this dependency in my pom:
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-messaging</artifactId>
</dependency>
What is wrong, why the SimpMessagingTemplate
bean doesn't exist ?
Okay so the bean SimpMessagingTemplate
is not found in your application context .So make sure that the bean is getting created by spring automatically in the application context at startup and the annotations are provided as necessary for the creation like :
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
}
Try using constructor injection like :
@Autowired
public WebSocketUserNotificationManagerImpl(SimpMessagingTemplate template) {
this.template = template;
}