I am trying to create a websocket client in spring boot. I have used WebsocketConnectionmanager to create client with handler but I noticed that the service with @Autowired annotation does not get created and the exception "messageService is null" is thrown. Given below are the files:
WebSocketConfig.java
@Configuration
public class WebSocketConfig {
@Autowired
private Environment environment;
@Bean
public WebSocketConnectionManager instrumentsWsConnectionManager() {
return getWebSocketConnectionManager(environment.getProperty("wsconfig.uri-prefix") + environment.getProperty("wsconfig.message-uri"), new MessageWsHandler());
}
privateWebSocketConnectionManager getWebSocketConnectionManager(String webSocketUri, WebSocketHandler webSocketHandler)
{
//Generates a web socket connection
WebSocketConnectionManager manager = new WebSocketConnectionManager(
new StandardWebSocketClient(),
webSocketHandler, //Must be defined to handle messages
webSocketUri);
//Will connect as soon as possible
manager.setAutoStartup(true);
return manager;
}
}
MessageWsHandler.java
@Component
public class MessageWsHandlerimplements WebSocketHandler {
@Autowired
MessageService messageService;
// Additional code
MessageService.java
@Service
public class MessageService {
// Service code
}