webservice与spring整合
转载地址:http://blog.****.net/mr_li13/article/details/51320738
前面的的提到了WebService整体开发过程,这里主要是说WebService与Spring的整合过程
一、提出问题
问题:在以往的项目中,我们都需要把项目发布到服务器上,而WebService也是一个服务,如果按照思想来说,我们需要发布两个服务才能使整个项目正常运行,而且端口号还不一样,这样服务两个、端口两个,这不是显得特别的复杂。有没有一个办法使它发布服务的时候,webService服务也能开启,端口号用的是一个。显然我们是有办法的,我们将它和spring进行整合,统一交给Spring进行管理即可。
二、整合过程:
1)spring管理业务系统的bean,beans.xml(spring基础)
2) WebService CXF cxf-servlet.xml(CXF服务文件)
3) 在web.xml中配置spring和cxf(这个肯定要在web.xml中加入撒)
1.考入jar包:这个jar包可以直接使用官网CXF下载的lib里面有spring的jar包,不用下载。点击下载链接
新建一个WebProject工程,将jar包考入lib下
2.新建一个CXFService服务类,用以开启服务的。
- package com.itcast.ws;
- import javax.jws.WebService;
- /*import org.apache.cxf.jaxws.JaxWsServerFactoryBean;*/
- @WebService(serviceName="CXFServices")
- public class CXFService {
- public String hello(String name){
- System.out.println("已经进入服务器:"+name);
- return "从服务器返回值:"+name;
- }
- /* public static void main(String[] args) {
- JaxWsServerFactoryBean bean = new JaxWsServerFactoryBean();
- bean.setAddress("http://localhost:9090/hello");
- bean.setServiceClass(CXFServer.class);
- bean.create();
- System.out.println("服务已经开启!");
- } */
- //传统的直接运行这个类就可以发布Service服务了。但是现在㤇直接和spring整个就不需要使用这个方式了
- }
3.1:我们先看看当cxf-servlet在WEB-INF下的时候配置:
- <?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:jaxws="http://cxf.apache.org/jaxws"
- xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
- <import resource="classpath:META-INF/cxf/cxf.xml"/>
- <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
- <!-- 有点儿类似JDK的形式 参数address是访问地址 implementor是实现者(最开始测试可以使用服务类com.itcast.ws.CXFService,但是后面和spring配置文件整合的时候就㤇使用bean的#ID了)-->
- <jaxws:endpoint implementor="com.itcast.ws.CXFService" address="/hello"/>
- </beans>
3.2.再看看当把cxf-servlet.xml和beans.xml在src下的时候配置:
- <?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:jaxws="http://cxf.apache.org/jaxws"
- xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
- <import resource="classpath:META-INF/cxf/cxf.xml"/>
- <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
- <!-- 有点儿类似JDK的形式 参数address是访问地址 implementor是实现者(最开始测试可以使用服务类com.itcast.ws.CXFService,但是后面和spring配置文件整合的时候就㤇使用bean的#ID了)-->
- <jaxws:endpoint implementor="#cxfServices" address="/hello"/>
- <import resource="beans.xml"/>
- </beans>
4.beans.xml文件(Spring )
- <?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:mvc="http://www.springframework.org/schema/mvc"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
- <!-- 这是Spring基础了,把服务类放进来,给spring管理 -->
- <bean id="cxfServices" class="com.itcast.ws.CXFService"></bean>
- </beans>
5.web.xml文件配置信息:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <display-name></display-name>
- <!-- 整合spring servlet -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:beans.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoader</listener-class>
- </listener>
- <!-- CXF和servlet整合 只管理路径为cxf(/cxf/*)下的内容 走一下源码就知道它会找cxf-servlet.xml文件-->
- <servlet>
- <servlet-name>cxf</servlet-name>
- <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
- <!-- 如果cxf-servlet.xml在web-inf下的话,就不用配置,但是如果在src下需要配置一下
- 名字为什么是config-location,看看CXFservlet源码就知道了。 -->
- <init-param>
- <param-name>config-location</param-name>
- <param-value>classpath:cxf-servlet.xml</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <!-- 访问WSDL文件的时候就需要访问这个名字,通过它可以找到web服务 -->
- <servlet-name>cxf</servlet-name>
- <url-pattern>/cxf/*</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
这里就要提出一个问题了?它通过cxf这个名字找到org.apache.cxf.transport.servlet.CXFServlet后,怎么找到cxf-servlet.xml文件的呢?我们追一下源码就知道了
tomcate运行下,看看结果:http://localhost:8080/CXFSpringProject/cxf
最后成功整合。