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(三)_第1张图片

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>

代码下载地址

你可能感兴趣的:(spring,Web,CXF,service)