Code Example:
@Component public class WebSocketHandlerMessage implements WebSocketHandler { @Autowired private BarrageMessageService barrageMessageService; }
Results show:
SpringBoot project integration webSocket, when the client establishes a connection with the server, it is foundbarrageMessageServiceThe object is not injected but is null.
Cause: Spring manages singletons and conflicts with websockets (multiple objects).
Detailed explanation: Initializes the project when starting, and the websocket will be initialized (not connected to the user). Spring will inject service into it. The service of the object is not null and is successfully injected. However, since spring manages singletons by default, service will only be injected once. When the client connects to the server, the server will create a new websocket object. At this time, the problem arises: spring manages singletons and will not inject service into the second websocket object, so as long as the websocket object is created by the user connection, it cannot be injected again.
For example, there is service in the controller and dao in the service. Because controller, service, and dao are singletons, null will not be reported during injection. However, websocket is not a singleton, so after using spring injection once, the subsequent objects will not be injected again, and a NullException will be reported.
Solution:
Solution 1: Use static, let the service belong to the class, and then inject the service of the class.
@Component public class WebSocketHandlerMessage implements WebSocketHandler { /** * Use static here to make the service belong to the class */ private static BarrageMessageService barrageMessageService; /** * When injecting, inject the service of the class */ @Autowired public void setBarrageMessageService(BarrageMessageService barrageMessageService) { = barrageMessageService; } }
Solution 2: Re-get from the Spring container when a new connection is establishedBarrageMessageServiceObject, so it can be used normally.
@Component public class WebSocketHandlerMessage implements WebSocketHandler { /** * Get the barrageMessageService object method * * @return */ public BarrageMessageService getMessageService() { return (); } /** * Get stringRedisTemplate object method * * @return */ public StringRedisTemplate getStringRedisTemplate() { return (); } }
SpringContext tool class method:
/** * @Description: SpringContext Get Spring context information * @Author: mingtian * @CreateDate: 2020/6/8 14:59 * @Version: 1.0 */ @Component public class SpringContext implements ApplicationContextAware { /** * Print log */ private Logger logger = (getClass()); /** * Get the context object */ private static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { = applicationContext; ("set applicationContext"); } /** * Get applicationContext * * @return */ public static ApplicationContext getApplicationContext() { return applicationContext; } /** * Get the bean object by name * * @param name * @return */ public static Object getBean(String name) { return getApplicationContext().getBean(name); } /** * Get bean object through class * * @param clazz * @param <T> * @return */ public static <T> T getBean(Class<T> clazz) { return getApplicationContext().getBean(clazz); } /** * Get the specified bean object through name, clazz * * @param name * @param clazz * @param <T> * @return */ public static <T> T getBean(String name, Class<T> clazz) { return getApplicationContext().getBean(name, clazz); } }
Both of the above solutions can solve the problem of service injection into null in webSocket.
This is the end of this article about using @Autowired to inject null in WebSocket. For more related WebSocket @Autowired to inject null, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!