Spialdor
Spialdor

Reputation: 1655

SimpMessagingTemplate no qualifying bean of type

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 ?

Upvotes: 2

Views: 4006

Answers (2)

Emeka
Emeka

Reputation: 61

public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer 

has been deprecated.

You can add dependency –

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

Then implement WebSocketMessageBrokerConfigurer

Upvotes: 0

Ananthapadmanabhan
Ananthapadmanabhan

Reputation: 6216

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;
}

Upvotes: 5

Related Questions