微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WebService之CXF+Spring集成使用注解

        本文介绍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();
		}
	}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐