(四)CXF整合Spring发布WebService服务

1.导入CXF相关的jar包和Spring相关的jar包,我建的是maven项目,就直接贴出maven依赖吧

<dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-rt-frontend-jaxws</artifactId>
	<version>2.7.11</version>
</dependency>
<dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-rt-transports-http-jetty</artifactId>
	<version>2.7.11</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-web</artifactId>
	<version>3.1.2.RELEASE</version>
</dependency>
2.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">
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:beans.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- CXF相关配置 -->
	<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>/cxf/*</url-pattern>
	</servlet-mapping>
</web-app>
3.接口

package cn.cjc.cxf;

import javax.jws.WebService;

@WebService
public interface IMessage {
	String getMsg(String username);
}
4.实现类

package cn.cjc.cxf;

import javax.jws.WebService;

@WebService(endpointInterface = "cn.cjc.cxf.IMessage")
public class MessageImpl implements IMessage {

	@Override
	public String getMsg(String username) {
		return username + ",欢迎学习CXF";
	}
}
5.beans.xml文件配置

<!-- 在原有的Spring配置文件内引入CXF的jaxws命名空间 -->
<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">
 	<!-- 引入CXF默认配置文件 -->
	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<bean id="message" class="cn.cjc.cxf.MessageImpl"/>
	<!-- 发布WS服务 -->
	<jaxws:server address="/mymsg"><!-- 最终的服务发布地址将是http://localhost:8080/cxfspring/cxf/mymsg -->
		<jaxws:serviceBean>
			<ref bean="message"/>
		</jaxws:serviceBean>
	</jaxws:server>
</beans>
6.发布WebService服务,启动WEB服务器的同时即发布服务

7.调用服务,新建一个工程,第一步肯定是用wsimport或者wsdl2java命令生成客户端调用代码,接下来既可以用纯代码的方式调用,也可以将服务接口配置成受Spring管理的Bean,通过获取Bean来调用,下面调用方式为后者

a)引入Spring和CXF相关jar包,maven依赖同1

b)beans.xml文件配置

<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">
	<jaxws:client id="message" serviceClass="cn.cjc.cxf.IMessage" address="http://localhost:8080/cxfspring/cxf/mymsg"/>
</beans>
c)调用

package cn.cjc.cxf;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Invoke {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
		IMessage service = context.getBean("message", IMessage.class);
		System.out.println(service.getMsg("Junki"));
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值