Webservice实践(七)CXF 与Spring结合+tomcat发布
2017-09-22 18:31
561 查看
上一节介绍了如何使用CXF 来发布服务,但是没有介绍使用web 容器来发布,很多项目需要用tomcat 这样的容器来发布。另外本节将介绍CXF 与spring 结合的方法。
2、通过tomcat 发布服务
2、myeclipse2015
3、使用spring 3.0 和cxf 2.7.6
注意里面需要勾选的地方
建议一路next 下去,而不是直接finish,在最后一页,把web.xml 勾选上,再finish
2、新建工程后,前面2节中涉及的java 类都copy 到当前工程里面,同时建立lib库,将相关的lib 添加到build path
源码结构如下图:
添加依赖的lib,结构如下
修改源码 StudentWsImpl.java 类,主要是去掉main方法中内容,因为现在的发布在tomcat容器中
[java] view plain copy
package com.study.cxfws.impl;
import javax.jws.WebService;
import com.study.cxfws.StudentWs;
import com.study.dao.StudentDAO;
import com.study.dao.impl.StudentDAOImpl;
@WebService
public class StudentWsImpl implements StudentWs {
//Student的dao 类,负责处理student 实体类的操作
private StudentDAO studentDAO;
public StudentWsImpl(){
studentDAO = new StudentDAOImpl();
}
public boolean addStudent(String name, String sex, String birthday) {
// 调用studentDAO.addStudent 方法入库
System.out.println("Now put student into DB!");
studentDAO.addStudent(name, sex, birthday);
return true;
}
public String queryStudent(String studentName) {
System.out.println("StudentWsImpl queryStudent->"+studentName);
Object tmp = studentDAO.queryStudent(studentName);
if (tmp== null) {
return "null";
} else {
return tmp.toString();
}
}
public static void main(String[] args) {
}
}
3、 在src 目录下,配置sping 的配置文件
在src 目录,新建一个sping的配置文件applicationContext-server.xml 名字可以根据自己习惯修改,内容如下
[html] view plain copy
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.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-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<span style="color:#ff0000;"> <bean id="StudentWsImpl" class="com.study.cxfws.impl.StudentWsImpl" />
<jaxws:server id="StudentSpingWs" serviceClass="com.study.cxfws.StudentWs"
address="/StudentSpingWs">
<jaxws:serviceBean>
<ref bean="StudentWsImpl" />
</jaxws:serviceBean>
</jaxws:server> </span>
</beans>
这里xml 配置文件里面, bean id 指定了web service 接口实现类,也就是调用接口后实际业务逻辑的实现类; server id 和address 是 web service接口暴露的地址,对应的是接口类 StudentWs, 而这个服务的serviceBean 指定为接口实现类,这样就指定了运行时调用的实现类。 这个配置也就是sping核心的ioc控制反转功能,通过配置文件将依赖的对象,用配置文件来实现依赖注入,从而实现控制反转。
4、修改web.xml 文件
[html] view plain copy
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>StudyWsSpringCXF</display-name>
<span style="color:#ff0000;"> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext-server.xml
</param-value>
</context-param></span>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<span style="color:#ff0000;"><servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping></span>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
context-param里面指定了spring 配置文件
5、发布到tomcat里面
在myeclipse里面,找到server,右键 tomcat 7.0
选择Add/Remove Deployments 后,在弹出对话框里面,选择本工程进行发布
6、 运行tomcat
运行后,如果在console 窗口,出现下面内容,说明tomcat 已经运行起来
[plain] view plain copy
二月 04, 2017 11:24:24 上午 org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
二月 04, 2017 11:24:24 上午 org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
二月 04, 2017 11:24:24 上午 org.apache.catalina.startup.Catalina start
INFO: Server startup in 14766 ms
7、查看发布的服务
通过下面url ,来查看发布的服务:http://localhost:8080/StudyWsSpringCXF/services 这里StudyWsSpringCXF是工程项目的名称,如果一切正常,打开的界面如下:
可以点击红框中内容获取wsdl 文档。
factory.setAddress("http://localhost:8080/StudyWsSpringCXF/services/StudentSpingWs");
1、建立工程、编写web service 服务端相应的代码,实现业务逻辑
2、编写sping的配置文件 applicationContext.xml 文件,这个文件放在src 的根目录下
3、编写配置web.xml 文件
4、发布到tomcat中、并进行调试
附:在本机环境下,执行10000次查询,关闭掉system打印,耗时:run time=37410 毫秒,平均每秒268次
一 目标:
1、利用sping的ioc特性,进行发布服务对象的配置2、通过tomcat 发布服务
二、环境准备
1、利用上一节的工程源码,做少量调整2、myeclipse2015
3、使用spring 3.0 和cxf 2.7.6
三、实践步骤
1、新建web project 工程注意里面需要勾选的地方
建议一路next 下去,而不是直接finish,在最后一页,把web.xml 勾选上,再finish
2、新建工程后,前面2节中涉及的java 类都copy 到当前工程里面,同时建立lib库,将相关的lib 添加到build path
源码结构如下图:
添加依赖的lib,结构如下
修改源码 StudentWsImpl.java 类,主要是去掉main方法中内容,因为现在的发布在tomcat容器中
[java] view plain copy
package com.study.cxfws.impl;
import javax.jws.WebService;
import com.study.cxfws.StudentWs;
import com.study.dao.StudentDAO;
import com.study.dao.impl.StudentDAOImpl;
@WebService
public class StudentWsImpl implements StudentWs {
//Student的dao 类,负责处理student 实体类的操作
private StudentDAO studentDAO;
public StudentWsImpl(){
studentDAO = new StudentDAOImpl();
}
public boolean addStudent(String name, String sex, String birthday) {
// 调用studentDAO.addStudent 方法入库
System.out.println("Now put student into DB!");
studentDAO.addStudent(name, sex, birthday);
return true;
}
public String queryStudent(String studentName) {
System.out.println("StudentWsImpl queryStudent->"+studentName);
Object tmp = studentDAO.queryStudent(studentName);
if (tmp== null) {
return "null";
} else {
return tmp.toString();
}
}
public static void main(String[] args) {
}
}
3、 在src 目录下,配置sping 的配置文件
在src 目录,新建一个sping的配置文件applicationContext-server.xml 名字可以根据自己习惯修改,内容如下
[html] view plain copy
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.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-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<span style="color:#ff0000;"> <bean id="StudentWsImpl" class="com.study.cxfws.impl.StudentWsImpl" />
<jaxws:server id="StudentSpingWs" serviceClass="com.study.cxfws.StudentWs"
address="/StudentSpingWs">
<jaxws:serviceBean>
<ref bean="StudentWsImpl" />
</jaxws:serviceBean>
</jaxws:server> </span>
</beans>
这里xml 配置文件里面, bean id 指定了web service 接口实现类,也就是调用接口后实际业务逻辑的实现类; server id 和address 是 web service接口暴露的地址,对应的是接口类 StudentWs, 而这个服务的serviceBean 指定为接口实现类,这样就指定了运行时调用的实现类。 这个配置也就是sping核心的ioc控制反转功能,通过配置文件将依赖的对象,用配置文件来实现依赖注入,从而实现控制反转。
4、修改web.xml 文件
[html] view plain copy
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>StudyWsSpringCXF</display-name>
<span style="color:#ff0000;"> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext-server.xml
</param-value>
</context-param></span>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<span style="color:#ff0000;"><servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping></span>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
context-param里面指定了spring 配置文件
5、发布到tomcat里面
在myeclipse里面,找到server,右键 tomcat 7.0
选择Add/Remove Deployments 后,在弹出对话框里面,选择本工程进行发布
6、 运行tomcat
运行后,如果在console 窗口,出现下面内容,说明tomcat 已经运行起来
[plain] view plain copy
二月 04, 2017 11:24:24 上午 org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
二月 04, 2017 11:24:24 上午 org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
二月 04, 2017 11:24:24 上午 org.apache.catalina.startup.Catalina start
INFO: Server startup in 14766 ms
7、查看发布的服务
通过下面url ,来查看发布的服务:http://localhost:8080/StudyWsSpringCXF/services 这里StudyWsSpringCXF是工程项目的名称,如果一切正常,打开的界面如下:
可以点击红框中内容获取wsdl 文档。
四、通过客户端验证
运行之前的客户端,验证一下发布的服务端能正常工作,需要注意的是,需要修改客户端类StudentWsClient.java里面调用服务端的地址,与当前发布的服务端一致factory.setAddress("http://localhost:8080/StudyWsSpringCXF/services/StudentSpingWs");
五、总结
通过sping+tomcat+cxf 发布webservice 服务的步骤如下:1、建立工程、编写web service 服务端相应的代码,实现业务逻辑
2、编写sping的配置文件 applicationContext.xml 文件,这个文件放在src 的根目录下
3、编写配置web.xml 文件
4、发布到tomcat中、并进行调试
附:在本机环境下,执行10000次查询,关闭掉system打印,耗时:run time=37410 毫秒,平均每秒268次
相关文章推荐
- Webservice实践(七)CXF 与Spring结合+tomcat发布
- CXF整合Spring通过tomcat发布webservice
- WebService -- Java 实现之 CXF ( 使用:Spring+CXF+Tomcat发布webService)
- webservice--cxf和spring结合,发布restFull风格的服务
- 【webservice】CXF结合spring发布简单的webservice服务
- Spring Boot结合cxf发布WebService接口jar包冲突的问题
- Spring与CXF结合发布WebService
- CXF实现简单webservice应用、集成spring发布到tomcat
- spring中发布基于cxf的webservice
- SpringBoot整合cxf发布WebService服务和客户端调用WebService服务
- Tomcat下通过CXF实现不用注解发布WebService
- Spring整合CXF之发布WebService服务
- cxf+spring发布webservice接口及接口的调用
- Spring 集成CXF框架发布Webservice服务 和 使用jdk生成Webservice clinet
- 解决cxf+spring发布的webservice缺少<wsdl:types>和<wsdl:message>标签的问题
- Spring整合CXF,发布RSETful 风格WebService
- Spring整合CXF发布WebService
- Spring+CXF+Maven发布Webservice
- CXF整合Spring发布WebService实例
- Spring整合CXF 发布webservice接口服务器(普通及REST)和客户端,WSDL简单解析