IntellijIdea finds my bin. But when I launch the application, an exception pops up when I try to connect a web socket. For some reason, my bean does not find it. I post the key pieces of code.
@Service @ServerEndpoint("/WebsocketHome/actions") public class WebSocketServer { private static final Logger LOG = Logger.getLogger(WebSocketServer.class); @Autowired private SessionHandler sessionHandler; // <-- img is from here @OnOpen public void onOpen(Session session) { LOG.error("Trying to open session"); LOG.error(sessionHandler.test()); // <-- exception is here } ... Bean:
@Component public class SessionHandler { private static final Logger LOG = Logger.getLogger(SessionHandler.class); private static Set<Session> sessions = Collections.synchronizedSet(new HashSet<Session>()); public void addSession(Session session) { LOG.error("sessionId = " + session.getId()); sessions.add(session); } public void removeSession(Session session) { LOG.error("Closed sessionId = " + session.getId()); sessions.remove(session); } public String test() { return "Test is ok"; } XML config:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath:application.properties" system-properties-mode="ENVIRONMENT"/> <context:component-scan base-package="com.exale.hms.repository"/> <context:component-scan base-package="com.exale.hms.utils"/> <context:component-scan base-package="com.exale.hms.websocket"/> <tx:annotation-driven transaction-manager="txManager"/> Stacktrace errors:
java.lang.NullPointerException com.exale.hms.websocket.WebSocketServer.onOpen(WebSocketServer.java:33) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:498) org.apache.tomcat.websocket.pojo.PojoEndpointBase.doOnOpen(PojoEndpointBase.java:65) org.apache.tomcat.websocket.pojo.PojoEndpointServer.onOpen(PojoEndpointServer.java:64) org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.init(WsHttpUpgradeHandler.java:133) org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:827) org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1419) org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:44) java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) java.lang.Thread.run(Thread.java:745) Both classes are in the same package.
Please help me figure it out. The second day I dig with this problem. Thank.