当前位置:首页 > 青鸟知识 > java

集成Sping与Web容器

来源:长沙北大青鸟新途 发布日期:2016-05-21
摘要:Apache的CXF此刻几近成了Java范畴构建Web Service的首选类库,而且它也确切俭朴易用,上面就经由过程几篇系列文章做一下俭朴先容

CXF如何和spring集成。

1.树立HelloWorld 接口类

package com.谷歌code.garbagecan.cxfstudy.helloworld;  

import javax.jws.WebMethod;  

import javax.jws.WebParam;  

import javax.jws.WebResult;  

import javax.jws.WebService;  

@WebService

public interface HelloWorld {  

   @WebMethod

   @WebResult String sayHi(@WebParam String text);  

}

2.树立HelloWorld完成类

package com.谷歌code.garbagecan.cxfstudy.helloworld;  

public class HelloWorldImpl implements HelloWorld {  

   public String sayHi(String name) {  

       String msg = Hello + name + ! ;  

       return msg;  

   }  

}

3.点窜web.xml文件

!DOCTYPE web-app PUBLIC  

-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN

//java.sun.com/dtd/web-app_2_3.dtd  

web-app  

    display-name cxfstudy /display-name  

    servlet  

        servlet-name cxf /servlet-name  

        servlet-class org.apache.cxf.transport.servlet.CXFServlet /servlet-class  

        load-on-startup 1 /load-on-startup  

    /servlet  

    servlet-mapping  

        servlet-name cxf /servlet-name  

        url-pattern /ws/* /url-pattern

    /servlet-mapping

    listener

        listener-class org.springframework.web.context.ContextLoaderListener /listener-class

    /listener

   

    context-param

        param-name contextConfigLocation /param-name

        param-value classpath*:**/spring.xml /param-value  

    /context-param  

     

/web-app

4.树立spring设置配备摆设文件并放在classpath途径下

?xml version= 1.0 encoding= UTF-8 ?  

beans xmlns= //www.springframework.org/schema/beans

   xmlns:xsi= //www.w3.org/2001/XMLSchema-instance xmlns:jaxws= //cxf.apache.org/jaxws

   xsi:schemaLocation= //www.springframework.org/schema/beans //www.springframework.org/schema/beans/spring-beans.xsd    

//cxf.apache.org/jaxws //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 /  

    jaxws:endpoint id= helloworld implementor= com.谷歌code.garbagecan.cxfstudy.helloworld.HelloWorldImpl address= /HelloWorld /  

     

    !-- For client test --  

    jaxws:client id= helloworldClient address= //localhost:9000/ws/HelloWorld service >

/beans  

5.树立测试类

package com.谷歌code.garbagecan.cxfstudy.helloworld;  

import org.springframework.context.ApplicationContext;  

import org.springframework.context.support.ClassPathXmlApplicationContext;  

public class SpringClient {  

   public static void main(String[] args) {  

       ApplicationContext context = new ClassPathXmlApplicationContext( spring.xml );  

       HelloWorld helloworld = (HelloWorld)context.getBean( helloworldClient );  

       System.out.println(helloworld.sayHi( kongxx ));  

   }  

}  

6.测试

6.1 起首启动tomcat或应用maven的jetty,并拜候//localhost:9000/ws/HelloWorld?wsdl来考证web service已启动而且失效;

6.2 然后运转测试类来考证web service。


拒绝套路 试听有礼

数据已加密保证您的信息安全