CXF是一個基於 Servlet 技術的 SOA 應用開發框架,簡單來講,就是WebService的輕量級實現。html
一、下載開發包:http://cxf.apache.org/download.html,選擇相應的版本。java
二、在eclipse建立maven web工程。web
三、打開開發包實例的:java_first_spring_support,將java,resources, webapp複製到工程。spring
四、複製pom.xml文件到工程,將工程名稱修改爲本身建立的工程,如:apache
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>jaxws</groupId> <artifactId>jaxws</artifactId> <version>0.0.1-SNAPSHOT</version> <name>jaxws Maven Webapp</name> <description>Spring HTTP Sample</description> <packaging>war</packaging> <properties> <cxf.version>${project.version}</cxf.version> <cxf.release.base>${basedir}/../..</cxf.release.base> </properties> <build> <plugins> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.1</version> <configuration> <webXml>src/main/webapp/WEB-INF/web.xml</webXml> </configuration> </plugin> </plugins> <finalName>JavaFirstSpringSupport</finalName> </build> <profiles> <profile> <id>server</id> <build> <defaultGoal>test</defaultGoal> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <executions> <execution> <phase>test</phase> <goals> <goal>java</goal> </goals> <configuration> <mainClass>demo.spring.service.Server</mainClass> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> <profile> <id>client</id> <build> <defaultGoal>test</defaultGoal> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <executions> <execution> <phase>test</phase> <goals> <goal>java</goal> </goals> <configuration> <mainClass>demo.spring.client.Client</mainClass> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> <dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>2.7.16</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>2.7.16</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>2.7.16</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.0.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.0.7.RELEASE</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-webapp</artifactId> <version>8.1.5.v20120716</version> </dependency> </dependencies> </project>
五、完成後目錄結構如圖:app
六、運行項目。框架
七、配置resouces中client_bean.xml中地址符合你本地的服務,運行Client.java,訪問成功。frontend
package demo.spring.client; import org.springframework.context.support.ClassPathXmlApplicationContext; import demo.spring.service.HelloWorld; public final class Client { private Client() { } public static void main(String args[]) throws Exception { // START SNIPPET: client ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"}); HelloWorld client = (HelloWorld)context.getBean("client"); String response = client.sayHi("Joe"); System.out.println("Response: " + response); System.exit(0); // END SNIPPET: client } }