Apache CXF = Celtix + Xfire 支持多种协议: SOAP1.1,1.2 XML/HTTP CORBA(Common Object Request Broker Architecture公共对象请求代理体系结构,早期语言使用的WS。C,c++,C#) 并可以与Spring进行快速无缝的整合 灵活的部署:可以运行在Tomcat,Jboss,Jetty(内置),IBMWS,BeaWL上面。可到Apache官网进行下载。
创建一个名为crm_service的web项目,把项目作为web服务发布到网络中,具体的配置过程如下:
1、导入cxf的lib目录下的jar包
2、在项目中提供CustomerService接口及其实现类,注意因为等一下我们需要用到web服务的接口,所有要在接口上提供@WebService注解,表明这是一个web服务
@WebService
public interface CustomerService {
public List<Customer> findAll();
}
@Transactional
public class CustomerServiceImpl implements CustomerService {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public List<Customer> findAll() {
String sql = "select * from t_customer";
List<Customer> list = jdbcTemplate.query(sql, new RowMapper<Customer>(){
public Customer mapRow(ResultSet rs, int arg1) throws SQLException {
int id = rs.getInt("id");//根据字段名称从结果集中获取对应的值
String name = rs.getString("name");
String station = rs.getString("station");
String telephone = rs.getString("telephone");
String address = rs.getString("address");
String decidedzone_id = rs.getString("decidedzone_id");
return new Customer(id, name, station, telephone, address, decidedzone_id);
}
});
return list;
}
3、在src目录下创建一个名为cxf.xml的配置文件,用来配置spring框架的内容和web服务的内容
<?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"
xmlns:soap="http://cxf.apache.org/bindings/soap"
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.xsd
http://cxf.apache.org/bindings/soap
http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- 引入CXF Bean定义如下,早期的版本中使用 -->
<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" />
<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///crm_service"/>
<property name="username" value="root"/>
<property name="password" value="123"/>
</bean>
<!-- 事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 支持事务注解 -->
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="customerService" class="com.iteason.crm.service.CustomerServiceImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<!-- 注册服务 -->
<jaxws:server id="myService" address="/customer">
<jaxws:serviceBean>
<ref bean="customerService"/>
</jaxws:serviceBean>
</jaxws:server>
</beans>
4、在web.xml中配置spring框架随项目的启动而加载配置文件cxf.xml,并指定web服务的映射路径
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:cxf.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
5、将该web服务用tomcat启动,并且在浏览器中输入http://localhost:8080/crm_service/service/customer?wsdl,看是否显示说明书,有则成功发布web服务
6、命令行中使用wsimport -s . http://localhost:8080/crm_service/service/customer?wsdl,删去所有的类和class,只留下一个服务的接口,即CustomerService接口
7、在bos项目中,导入cxf的jar包(maven项目直接配置依赖)
8、在bos项目的applicationContext.xml中配置注册crm客户端代理对象
<!-- 注册crm客户端代理对象 -->
<jaxws:client id="crmClient"
serviceClass="com.iteason.crm.service.CustomerService"
address="http://192.168.1.180:8080/crm_service/service/customer"/>
9、这样就把CustomerService这个接口给配置好了,在web层可以正常使用了