使用XML配置spring-websocket服务

前端之家收集整理的这篇文章主要介绍了使用XML配置spring-websocket服务前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

使用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

猜你在找的XML相关文章