spring websocket tomcat was websphere9.0 Multiple Endpoints may not be deployed to the same path
阿新 • • 發佈:2020-08-24
網上說給 去掉@Configuration , 註釋掉 ServerEndpointExporter ,但是 不同環境 不可能 程式碼亂搞,所以明顯是不行的。
下面貼程式碼: 主要在註冊bean時候 ,做了個判斷@Conditional(value=WebSocketCondition.class)
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration;import org.springframework.stereotype.Component; import org.springframework.web.socket.server.standard.ServerEndpointExporter; import org.springframework.web.socket.server.standard.ServerEndpointRegistration; @Configuration //@Component public class EndpointConfig { @Bean public ServerEndpointExporter endpointExporter() {return new ServerEndpointExporter(); } /** * 在websphere種,無法完成自動註冊websocket。 * 判斷WebSocketCondition 是否註冊,未註冊則執行 * @return */ @Conditional(value=WebSocketCondition.class) @Bean public EchoAnnotatedEndpoint echoAnnotatedEndpoint() { return new EchoAnnotatedEndpoint(echoService()); } @Beanpublic EchoService echoService() { return new DefaultEchoService(); } }
WebSocketCondition
import javax.servlet.ServletContext; import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.type.AnnotatedTypeMetadata; import org.springframework.web.context.support.XmlWebApplicationContext; public class WebSocketCondition implements Condition{ @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { XmlWebApplicationContext resourceLoader = (XmlWebApplicationContext) context.getResourceLoader(); ServletContext servletContext = resourceLoader.getServletContext(); String serverInfo = servletContext.getServerInfo(); //tomcat 容器 ,這裡可以根據實際業務,自己判斷 if(serverInfo != null && serverInfo.contains("Tomcat")) { return false; } return true; } }
OK, 問題解決, 說白了,就是bean註冊了兩次而已