加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

WebService之CXF+Spring集成(使用注解)

发布时间:2020-12-17 00:15:26 所属栏目:安全 来源:网络整理
导读:? ? ? ? 本文介绍CXF+Spring使用注解的集成,用到的接口等java类与XFire+Spring整合相同,不再给出,只给出配置信息。使用CXF版本为2.6.2,Spring为3.1.1,测试环境为Tomcat6.0。CXF2.6.2下载 ? ? ? ??1.建立一个Web工程,引入相应的jar包,最精简jar包下载

? ? ? ? 本文介绍CXF+Spring使用注解的集成,用到的接口等java类与XFire+Spring整合相同,不再给出,只给出配置信息。使用CXF版本为2.6.2,Spring为3.1.1,测试环境为Tomcat6.0。CXF2.6.2下载

? ? ? ??1.建立一个Web工程,引入相应的jar包,最精简jar包下载:

commons-logging-1.1.1.jar
cxf-2.6.2.jar
geronimo-jaxws_2.2_spec-1.1.jar
jaxb-api-2.2.6.jar
neethi-3.0.2.jar
org.apache.cxf.spring.remoting.Jsr181HandlerMapping.jar
org.springframework.aop-3.1.1.RELEASE.jar
org.springframework.asm-3.1.1.RELEASE.jar
org.springframework.beans-3.1.1.RELEASE.jar
org.springframework.context-3.1.1.RELEASE.jar
org.springframework.core-3.1.1.RELEASE.jar
org.springframework.expression-3.1.1.RELEASE.jar
org.springframework.web.servlet-3.1.1.RELEASE.jar
org.springframework.web-3.1.1.RELEASE.jar
wsdl4j-1.6.2.jar
xmlschema-core-2.0.1.jar

? ? ? ??2.修改web.xml,加入以下代码:

	<listener>
	    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<context-param>
	    <param-name>contextConfigLocation</param-name>
	    <param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	
 	<servlet>
	    <servlet-name>CXFService</servlet-name>
	    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
	    <load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
	    <servlet-name>CXFService</servlet-name>
	    <url-pattern>/service/*</url-pattern>
	</servlet-mapping>
? ? ? ? 3.在classpath下加入Spring配置文件applicationContext.xml,加入以下代码, 注意component-scan一定要在三个import之后,否则启动服务会报错

	<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" />
	
	<context:component-scan base-package="my" />
	
	<bean id="webServicesAgent" class="org.apache.cxf.spring.remoting.Jsr181HandlerMapping">
		<property name="urlPrefix"><value>/</value></property>
	</bean>
? ? ? ? 4.服务端配置完成,接口定义及其他类不再给出,下面是客户端调用:

	@Test
	public void testCXFBookService() {
		try {
			JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
			factory.setServiceClass(IBookService.class);
			factory.setAddress("http://127.0.0.1:8080/CXFTest/service/BookService");
			IBookService bookService = (IBookService) factory.create();
			System.out.println(">>>>>>>>Client: " + bookService.getBook());
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读