Apache CXF 与 Spring 整合简单例子

阅读更多

1、使用MyEclipse 创建Web  Project ,添加  maven 支持。 生成项目结构如下:

 

Apache CXF 与 Spring 整合简单例子_第1张图片

 

2、POM.XML 文件内容如下 。(包含  cxf 与 spring 等的jar包)

 


	4.0.0
	cxf
	cxf
	war
	0.0.1-SNAPSHOT
	cxf Maven Webapp
	http://maven.apache.org


	
		2.4.3
		2.5.6
	
	
		
			org.apache.cxf
			cxf-rt-frontend-jaxws
			${cxf.version}
		
		
			org.apache.cxf
			cxf-rt-transports-http
			${cxf.version}
		
		
		
			org.apache.cxf
			cxf-rt-transports-http-jetty
			${cxf.version}
		
		
			org.apache.cxf
			cxf-tools-common
			${cxf.version}
		
		
			org.apache.cxf
			cxf-tools-java2ws
			${cxf.version}
		
		
			org.apache.cxf
			cxf-tools-validator
			${cxf.version}
		
		
			org.apache.cxf
			cxf-tools-wsdlto-core
			${cxf.version}
		
		
			org.apache.cxf
			cxf-tools-wsdlto-databinding-jaxb
			${cxf.version}
		
		
			org.springframework
			spring-webmvc
			${spring.version}
			jar
			compile
			
				
					spring-web
					org.springframework
				
			
		
		
			org.springframework
			spring
			${spring.version}
			jar
			compile
			
				
					commons-logging
					commons-logging
				
			
		
		
			org.apache.cxf
			cxf-tools-wsdlto-frontend-jaxws
			${cxf.version}
		
	
	

		
			apache-snapshots
			Apache SNAPSHOT Repository
			http://repository.apache.org/snapshots/
			
				true
			
		
		
			atlassian-m2-repository
			https://m2proxy.atlassian.com/repository/public
		
	

	
		
			
				org.apache.cxf
				cxf-java2ws-plugin
				${cxf.version}
			
		
	

 3、创建服务接口类与实现类。(JDK 默认是1.6 u13 的,最好更换为 1.6 u 17以上)

 

package demo;

import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

@WebService
@SOAPBinding(style = Style.RPC)
public interface IHelloWorld {
	
	public String sayHello(@WebParam(name = "name") String name);

}

 package demo;

public class HelloWorldImpl implements IHelloWorld{

	public String sayHello(String name) {
		 return name + " say: Hello World!";
	}
}
 

 

 4、Spring 配置文件

 


       
        
 
    
    
    





    



 
 
      
        
              
              
        
    
  

 

5、WEB.XML 文件

 

 


  	
  
    
    
        contextConfigLocation
        /Cxf-config.xml
    
    
    
        
            org.springframework.web.context.ContextLoaderListener
        
    
  
    
    
        CXFServlet
        
            org.apache.cxf.transport.servlet.CXFServlet
        
        1
    
    
    
        CXFServlet
        /*
    
  
  
    index.jsp
  

 

6、发布并访问 http://localhost:9098/mycxf/helloWorld?wsdl 。

 

7、客户端Spring调用测试

 

8、客户端 Spring 文件 

 

 


        
    
    
    


    
    
        

 9、客户端测试代码

 

 

package client;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import demo.IHelloWorld;

/**
 * 客户端调用,测试代码
 */
public class HelloServiceClient {
	public static void main(String[] args) {
		// 加载客户端的配置定义
		ApplicationContext context = new ClassPathXmlApplicationContext("Cxf-client.xml");
		// 获取定义的 Web Service Bean
		IHelloWorld helloService = (IHelloWorld) context.getBean("helloServiceClient");
		String username = "Ailan";
		// 调用方法进行服务消费
		String result = helloService.sayHello(username);
		System.out.println("Result:" + result);
	}
}

 10、最终项目结构

Apache CXF 与 Spring 整合简单例子_第2张图片

11、附件中为完整代码。

 

 

  • mycxf.zip (14 KB)
  • 下载次数: 696

你可能感兴趣的:(Apache CXF 与 Spring 整合简单例子)