maven spring集成cxf

//maven pom文件
<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>com.liyi.lianxi</groupId>
  <artifactId>hellocxf1</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>hellocxf1 Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>
    
            <!-- CXF Dependencies -->  
     <dependency>  
         <groupId>org.apache.cxf</groupId>  
         <artifactId>cxf-rt-frontend-jaxws</artifactId>  
         <version>${cxf.version}</version>  
     </dependency>  
     <dependency>  
         <groupId>org.apache.cxf</groupId>  
         <artifactId>cxf-rt-transports-http</artifactId>  
         <version>${cxf.version}</version>  
     </dependency>  
     <!-- Spring Dependencies ${spring.version} -->  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-context</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-web</artifactId>  
            <version>${spring.version}</version>  
        </dependency>  
  
        <dependency>  
            <groupId>org.slf4j</groupId>  
            <artifactId>slf4j-log4j12</artifactId>  
            <version>${slf4j.version}</version>  
            <type>jar</type>  
            <scope>compile</scope>  
        </dependency>  
  </dependencies>
  <build>
          <plugins>
               <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-compiler-plugin</artifactId>  
                <configuration>  
                    <source>1.6</source>  
                    <target>1.6</target>  
                    <encoding>UTF-8</encoding>  
                </configuration>  
            </plugin>  
            <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-surefire-plugin</artifactId>  
                <configuration>  
                    <!-- 跳過測試單元 -->  
                    <skip>true</skip>  
                </configuration>  
            </plugin>  
          </plugins>
    <finalName>hellocxf1</finalName>
  </build>
  
  <properties>  
        <junit.version>4.11</junit.version>  
        <cxf.version>2.2.3</cxf.version>  
        <spring.version>3.2.3.RELEASE</spring.version>  
        <slf4j.version>1.7.7</slf4j.version>  
    </properties>  
</project>

//spring 核心配置文件
<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.0.xsd  
    http://cxf.apache.org/jaxws   
    http://cxf.apache.org/schemas/jaxws.xsd">  
      
    <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" />  
    <!-- 要暴露給外部調用的接口,address:請求路徑 -->  
    <jaxws:endpoint  implementor="com.liyi.webservice.HelloCxfImpl" address="/HelloCxf">
    </jaxws:endpoint>  
      
</beans>  

//接口
package com.liyi.webservice;

import javax.jws.WebService;

@WebService
public interface HelloCxf {
    public void sayHello();
}
//實現類
package com.liyi.webservice;

import javax.jws.WebService;

@WebService(endpointInterface = "com.liyi.webservice.HelloCxf")  
public class HelloCxfImpl implements HelloCxf{

    @Override
    public void sayHello() {
        // TODO Auto-generated method stub
        System.out.println("helloCxf");
        
    }

}

//啓動項目 訪問 如訪問成功,則服務器端代碼編寫完畢


//配置測試xml
<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.0.xsd  
    http://cxf.apache.org/jaxws  
    http://cxf.apache.org/schemas/jaxws.xsd">  
      
    <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" />  
      
    <!-- serviceClass:要訪問的接口,address:接口地址  這裏不要配置?wsdl否則會報錯-->  
    <jaxws:client id="helloCxfClient" serviceClass="com.liyi.webservice.HelloCxf"  
        address="http://127.0.0.1:8080/hellocxf1/services/HelloCxf" />  
</beans> 

//把服務器端接口拷貝過來
package com.liyi.webservice;

import javax.jws.WebService;

@WebService
public interface HelloCxf {
    public void sayHello();
}

//編寫測試代碼
package com.liyi.test;

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

import com.liyi.webservice.HelloCxf;

public class TestCxf {
    public static void main(String[] args) {
         //加載Spring配置文件  
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-client.xml"); 
       HelloCxf helloCxf= context.getBean("helloCxfClient",HelloCxf.class);
       helloCxf.sayHello();
    }
}

//run一下 就能夠調到服務器端的方法
相關文章
相關標籤/搜索