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一下 就可以调到服务器端的方法


转载于:https://my.oschina.net/u/1998885/blog/467212

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值