熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

CXF與spring集成

2022-06-13   來源: Java開源技術 

  . 新建web project 並加入apachecxf\lib所有包編寫要發布的web service 接口和實現這一步與前面一樣

  import javaxjwsWebService;

  @WebService

  public interface HelloWorld {

  public String sayHello(String text);

  }

  import javaxjwsWebService;

  @WebService(endpointInterface=testHelloWorld)

  public class HelloWorldImpl implements HelloWorld {

  public String sayHello(String text) {

  return Hello + text ;

  }

  }

  @WebService 注解表示是要發布的web 服務

   在springcxfxml配置發布的web service

  <?xml version= encoding=UTF?>

  <beans xmlns=

  xmlns:xsi=instance

  xmlns:jaxws=

  xsi:schemaLocation=

  beansxsd

  >

  <import resource=classpath:METAINF/cxf/cxfxml />

  <import resource=classpath:METAINF/cxf/cxfextensionsoapxml />

  <import resource=classpath:METAINF/cxf/cxfservletxml />

  <bean id=hello class=testHelloWorldImpl />

  <jaxws:endpoint id=helloWorld implementor=#hello

  address=/HelloWorld />

  </beans>

  注意<jaxws:endpoint id=helloWorld implementor=#hello

  address=/HelloWorld />

  id指在spring配置的bean的ID

  Implementor:指明具體的實現類

  Address:指明這個web service的相對地址

  .  配置webxml文件

  <?xml version= encoding=UTF?>

  <webapp version= xmlns=

  xmlns:xsi=instance

  xsi:schemaLocation=

  app__xsd>

  <contextparam>

  <paramname>contextConfigLocation</paramname>

  <paramvalue>classpath:springcxfxml</paramvalue>

  </contextparam>

  <listener>

  <listenerclass>

  orgsprntextContextLoaderListener

  </listenerclass>

  </listener>

  <servlet>

  <servletname>CXFServlet</servletname>

  <servletclass>

  orgapachecxftransportservletCXFServlet

  </servletclass>

  <loadonstartup></loadonstartup>

  </servlet>

  <servletmapping>

  <servletname>CXFServlet</servletname>

  <urlpattern>/*</urlpattern>

  </servletmapping>

  </webapp>

  .部署到tomcat服務器輸入//localhost:/<webappname>/ HelloWorld?wsdl將顯示這個web service的wsdl

  注意如果webxml配置<servletname>CXFServlet</servletname>

  <urlpattern>/ws/*</urlpattern>

  則訪問地址為/ws///localhost:/<webappname>/ws/ HelloWorld?wsdl

   下面就開始創建一個客戶端程序訪問這個web service 同樣新建java project 並加入apachecxf\lib所有包 創建與具體webservice技術無關的業務接口HelloWorldjava

  public interface HelloWorld {

  public String sayHello(String text);

  }

  配置springclientxml

  <beans xmlns=

  xmlns:xsi=instance

  xmlns:jaxws=

  xsi:schemaLocation=

  beansxsd

  >

  <bean id=client class=testHelloWorld

  factorybean=clientFactory factorymethod=create/>

  <bean id=clientFactory class=orgapachecxfjaxwsJaxWsProxyFactoryBean>

  <property name=serviceClass value=testHelloWorld/>

  <property name=address value=//localhost:/helloWorld/>

  <! 這個地方的地址一定要注意正確的>

  </bean>

  </beans>

  .測試

  import orgntextApplicationContext;

  import orgntextsupportClassPathXmlApplicationContext;

  import testHelloWorld;

  public class Test {

  public static void main(String[] args) {

  ApplicationContext ctx = new ClassPathXmlApplicationContext(

  springclientxml);

  HelloWorld client = (HelloWorld) ctxgetBean(client);

  String result = clientsayHello(你好!);

  Systemoutprintln(result);

  }

  }


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28940.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.