在使用spring-websocket包搭建websocket服务一文中我们使用的是注解方式的来配置websocket。
下面我来将其改成xml方式的配置
一。更新Spring的xml命名空间,使其支持WebSocket
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:websocket="http://www.springframework.org/schema/websocket" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket.xsd">
namespace其中添加了
xmlns:websocket="http://www.springframework.org/schema/websocket" http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket.xsd"
二。在context:component-scan 取消对WebSocketConfig的扫描
三。配置websocket:handlers
<bean id="wsHandler" class="org.jstudioframework.websoket.handler.WebSocketHandler"/> <websocket:handlers> <websocket:mapping path="/websocket/socketServer.do" handler="wsHandler"/> <websocket:handshake-interceptors> <bean class="org.jstudioframework.websoket.interceptor.WebSocketHandshakeInterceptor"/> </websocket:handshake-interceptors> </websocket:handlers>
四,完整配置
<?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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:websocket="http://www.springframework.org/schema/websocket" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket.xsd"> <context:annotation-config/> <mvc:annotation-driven/> <context:component-scan base-package="org.jstudioframework.websoket.controller"/> <bean id="wsHandler" class="org.jstudioframework.websoket.handler.WebSocketHandler"/> <websocket:handlers> <websocket:mapping path="/websocket/socketServer.do" handler="wsHandler"/> <websocket:handshake-interceptors> <bean class="org.jstudioframework.websoket.interceptor.WebSocketHandshakeInterceptor"/> </websocket:handshake-interceptors> </websocket:handlers> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> </beans>五,运行测试,效果一样 原文链接:https://www.f2er.com/xml/293919.html