Auto Install SimpMessagingTemplate

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;

    // Some Code

    public void tellUser(String username, String url) {
        // This is always true
        System.out.println("TEMPLATE NULL? " +(this.template == null));
        // Further code omitted
    }
}

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) {
        // False here, template is autowired correctly
        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.

+4
source share
1

( SimpMessagingTemplate , ), spring .

beans? , ? beans XML?

0

Source: https://habr.com/ru/post/1615307/


All Articles