CXF實戰之集成Spring(三)

CXF原生支持Spring,可以和Spring無縫集成。WebService框架CXF實戰一在Tomcat中發佈WebService(二)通過Spring Web實現CXFServlet。下面將Spring和CXF集成在一起,CXF發佈的WebService可以調用Spring的Bean。
創建Maven Web項目,在pom.xml中添加CXF和Spring的引用,由於該Web項目中不涉及數據庫,沒有添加Spring JDBC、Spring ORM等數據庫相關模塊。

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.rvho</groupId>
    <artifactId>cxfserver</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <!-- CXF版本 -->
        <cxf.version>3.1.1</cxf.version>
        <!-- Spring版本 -->
        <spring.version>4.1.7.RELEASE</spring.version>
    </properties>

    <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- End Spring -->

        <!-- CXF -->
        <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>
        <!-- End CXF -->
    </dependencies>
</project>

在項目中添加Dao接口及實現類,WebService調用Dao層數據,項目結構如圖所示。
CXF集成Spring

HelloDao接口,提供welcome方法。

package com.rvho.cxfserver.dao;

public interface HelloDao {
    String welcome(String name);
}

HelloDao接口實現類HelloDaoImpl

package com.rvho.cxfserver.dao.impl;

import org.springframework.stereotype.Repository;

import com.rvho.cxfserver.dao.HelloDao;

@Repository("helloDao")
public class HelloDaoImpl implements HelloDao {

    @Override
    public String welcome(String name) {
        return "歡迎使用CXF!" + name;
    }
}

HelloWS接口,由於集成了Spring,該接口既是WebService,也是Spring Service。

package com.rvho.cxfserver.ws;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService(
        name = "HelloWS",
        targetNamespace = "http://www.tmp.com/services/hello"
    )
public interface HelloWS {
    @WebMethod
    String welcome(@WebParam(name = "name") String name);
}

HelloWS接口實現類HelloWSImpl,實現類調用HelloDaoImpl的方法。

package com.rvho.cxfserver.ws.impl;

import javax.annotation.Resource;
import javax.jws.WebService;

import org.springframework.stereotype.Service;

import com.rvho.cxfserver.dao.HelloDao;
import com.rvho.cxfserver.ws.HelloWS;

@WebService(
        endpointInterface = "com.rvho.cxfserver.ws.HelloWS",
        portName = "HelloWSPort",
        serviceName = "HelloWSService",
        targetNamespace = "http://www.tmp.com/services/hello"
    )
@Service("helloWS")
public class HelloWSImpl implements HelloWS {
    @Resource
    private HelloDao helloDao;

    @Override
    public String welcome(String name) {
        return helloDao.welcome(name);
    }
}

Spring上下文配置

<?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:aop="http://www.springframework.org/schema/aop" 
    xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <!-- <context:annotation-config /> -->

    <!-- 使Spring支持自動檢測組件,如註解的Repository、Service、Controller -->
    <context:component-scan base-package="com.rvho" />
</beans>

在WEB-INF下創建cxf-servlet.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"
    xmlns:soap="http://cxf.apache.org/bindings/soap"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
        http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd 
        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

    <!-- helloWS通過註解獲取 -->
    <jaxws:endpoint id="helloWSEndpoint" implementor="#helloWS" address="/hello"></jaxws:endpoint>
</beans>

在web.xml中添加Spring上下文Listener以及CXFServlet

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>cxfserver</display-name>

    <!-- Spring配置 -->   
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-context.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- End Spring配置 -->

    <!-- CXF Servlet -->
    <servlet>
        <servlet-name>cxfservlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>cxfservlet</servlet-name>     
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>
    <!-- End CXF Servlet -->    

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

代碼下載地址

發佈了91 篇原創文章 · 獲贊 40 · 訪問量 86萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章