0

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

}

Siddharth
  • 13
  • 5
  • Does this answer your question? [Why is my Spring @Autowired field null?](https://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null) – dunni Dec 05 '21 at 11:20
  • That worked, I had created an instance of a message handler using "new" operator instead of Autowiring it. – Siddharth Dec 21 '21 at 14:01

0 Answers0