I'm having problems using SimpMessagingTemplate in the Service class. Here are the relevant code snippets:
UserService.java . Error Autowiring, template = null
@Service
public class UserService{
@Autowired
private SimpMessagingTemplate template;
public void tellUser(String username, String url) {
System.out.println("TEMPLATE NULL? " +(this.template == null));
}
}
SocketController.java . Work Autowiring, Spring Works with Websockets as built-in.
@Controller
public class WebSocketController {
@Autowired
private SimpMessagingTemplate template;
public void tellUser(String username, String url) {
System.out.println("TEMPLATE NULL? " +(this.template == null));
this.template.convertAndSendToUser(username, "/test/notify", new Greeting("USER SPECIFIC MESSAGE: " + url));
}
}
WebSocketConfig.java
@Configuration
@EnableScheduling
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/user", "/test");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/test").withSockJS();
}
}
So basically auto-install works in my Controller class, but not in my service. I need to call convertAndSendToUser manually in a service. What is the difference for an Autowiring SimpMessagingTemplate between a controller and a service? Any data on why this might happen is very welcome.
source
share