Spring springboot websocket recording process can not be injected (@Autowired) to solve the problem

When using WebSocket @service annotation service classes start no problem, when sending a chat message, unusual: java.lang.NullPointException, the process to find a lot of solutions, but these methods are not resolved, there will be other some mistakes.
Finally resolved, Citie recorded for later inspection.

The first embodiment: the configurator = SpringConfigurator.class add annotations later @ServerEndpoint

as follows

@ServerEndpoint(value = "/websocket/{user}", configurator = SpringConfigurator.class)
@Controller

The following error occurs in this way, from the literal meaning is ContextLoaderListener not running.

java.lang.IllegalStateException: Failed to find the root WebApplicationContext. Was ContextLoaderListener not used?

ContextLoaderListener is spring MVC web.xml configuration file, with Spring boot relationship does not seem, there is no depth in a different way.

The second way: configure websock class configuration class in websock

as follows:

@Configuration
public class EndpointConfig {
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        ServerEndpointExporter serverEndpointExporter = new ServerEndpointExporter();
        serverEndpointExporter.setAnnotatedEndpointClasses(WebsocketController.class);
        return serverEndpointExporter;
    }
}

WebsocketController websocket implementation class is still unresolved.

The third way: Class rewrite ApplicationContextAware

as follows:

@Component
public class SpringUtil implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if(SpringUtil .applicationContext == null) {
            SpringUtil .applicationContext = applicationContext;
        }
    }
    public static ApplicationContext getApplicationContext(){
        return applicationContext;
    }

    public static Object getBean(String beanName){
        return applicationContext.getBean(beanName);
    }

    public static <T> T getBean(Class<T> clazz){
        return (T)applicationContext.getBean(clazz);
    }

}

Then, to obtain Service methods getBean

private static WeChatService weChatService = SpringUtil .getBean(WeChatService.class);

This method seems very feasible ah, start, error ...

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'websocketController' defined in file

bean creation fails, is preparing to overcome this problem, accidentally we found another solution.

The fourth way: Service class initialization, and then assigned by @Autowired

as follows:

	private static WeChatService weChatService ;

	@Autowired
	public void setWeChatService(WeChatService weChatService){
		this.weChatService = weChatService;
	}

So far the problem is solved.

But the third way in the end may not be feasible, the message needs to be big brothers! ! !

end!

Published 24 original articles · won praise 11 · views 5422

Guess you like

Origin blog.csdn.net/weixin_44037376/article/details/97644830