CXF与Spring整合入门示例

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014248473/article/details/87874803

CXF与Spring整合入门示例

一、环境介绍

cxf:2.6.2
jdk:1.7
ide:IDEA14
tomcat:8.0.35
(-)服务端在spring配置文件中配置jaxws:server发布服务
(-)客户端使用wsdl2java工具生成(需将cxf配置环境变量)
(-)客户端spring配置文件中配置jaxws:client调用服务
(-)所用jar都来自cxf的lib目录

 CXF与Spring整合工程目录结构

二、服务端

1.jar包

asm-3.3.1.jar
commons-logging-1.1.1.jar
cxf-2.6.2.jar
geronimo-servlet_2.5_spec-1.1.2.jar
neethi-3.0.2.jar
wsdl4j-1.6.2.jar
xmlschema-core-2.0.3.jar

spring-aop-3.0.7.RELEASE.jar
spring-asm-3.0.7.RELEASE.jar
spring-beans-3.0.7.RELEASE.jar
spring-context-3.0.7.RELEASE.jar
spring-core-3.0.7.RELEASE.jar
spring-expression-3.0.7.RELEASE.jar
spring-tx-3.0.7.RELEASE.jar
spring-web-3.0.7.RELEASE.jar

2.接口

package com.yale.service;

import javax.jws.WebService;

@WebService
public interface HelloWorld {
    String sayHello(String name);
}

3.接口实现

package com.yale.service;

public class HelloWorldImpl implements HelloWorld {

    @Override
    public String sayHello(String name){
        System.out.println("sayHello方法被调用");
        return ("Hello "+name);
    }

}

4.发布

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans.xsd
              http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

       <bean name="helloWorld" class="com.yale.service.HelloWorldImpl"/>

       <jaxws:server address="/hello" serviceClass="com.yale.service.HelloWorld">
              <jaxws:serviceBean>
                     <ref bean="helloWorld"/>
              </jaxws:serviceBean>
       </jaxws:server>
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <!-- 设置spring的环境 -->
    <context-param>
        <!--contextConfigLocation是不能修改的 -->
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 配置CXF的Servlet -->
    <servlet>
        <servlet-name>CXF</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>CXF</servlet-name>
        <url-pattern>/ws/*</url-pattern>
    </servlet-mapping>

</web-app>

5.测试

配置tomcat,选中你的Artifact,配置上下文路径Application context: /
启动tomcat
浏览器打开:http://localhost:8080/ws/hello?wsdl

三、客户端

1.jar包

直接将服务端的lib复制过来

2.wsdl2java生成客户端

- 新建一个Java项目
- 在src目录下进入cmd窗口
- 输入命令:wsdl2java -d . -p com.yale.service http://localhost:8080/ws/hello?wsdl

3.Spring配置文件

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans.xsd
              http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

       <jaxws:client id="helloClient"
                     address="http://localhost:8080/ws/hello"
                     serviceClass="com.yale.service.HelloWorld" />
</beans>

4.调用

package com.yale.client;

import com.yale.service.HelloWorld;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ClientMain {
    public static void main(String[] args) {
        // 初始化spring的上下文
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        // 调用查询方法
        HelloWorld helloWorld = (HelloWorld) context.getBean("helloClient");
        System.out.println(helloWorld.sayHello("Rose"));
    }
}

猜你喜欢

转载自blog.csdn.net/u014248473/article/details/87874803