CXF結合Spring開發中,如果既有CXFServlet和DispatcherServlet,一定要先開啓org.springframework.web.context.ContextLoaderListener這個監聽器,然後加載DispatcherServlet完之後再加載CXFServlet。
具體看web.xml配置:
<!-- 啓動spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:app-context.xml</param-value>
</context-param>
<!-- 配置CXF -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
<!-- 配置spring mvc -->
<servlet>
<servlet-name>springapp</servlet-name>
<servlet-class>com.market.servlet.MyDispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:app-web.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
其中com.market.servlet.MyDispatcherServlet是自定義的servlet,是繼承org.springframework.web.servlet.DispatcherServlet的servlet