知识大全 CXF与spring集成

Posted 地址

篇首语:没有伞的孩子必须学会努力奔跑。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 CXF与spring集成相关的知识,希望对你有一定的参考价值。

CXF与spring集成  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

   . 新建web project 并加入apache cxf \\lib所有包 编写要发布的web service 接口和实现 这一步 与前面一样

  import javax jws WebService;

  @WebService

  public interface HelloWorld

  public String sayHello(String text);

  

  import javax jws WebService;

  @WebService(endpointInterface= test HelloWorld )

  public class HelloWorldImpl implements HelloWorld

  public String sayHello(String text)

  return Hello + text ;

  

  

  @WebService 注解表示是要发布的web 服务

   在spring cxf xml配置发布的web service

  <?xml version= encoding= UTF ?>

  <beans xmlns=

  xmlns:xsi= instance

  xmlns:jaxws=

  xsi:schemaLocation=

   beans 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 />

  <bean id= hello class= test HelloWorldImpl />

  <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的相对地址

   .  配置web xml文件

  <?xml version= encoding= UTF ?>

  <web app version= xmlns=

  xmlns:xsi= instance

  xsi:schemaLocation=

   app_ _ xsd >

  <context param>

  <param name>contextConfigLocation</param name>

  <param value>classpath:spring cxf xml</param value>

  </context param>

  <listener>

  <listener class>

   sprntext ContextLoaderListener

  </listener class>

  </listener>

  <servlet>

  <servlet name>CXFServlet</servlet name>

  <servlet class>

   apache cxf transport servlet CXFServlet

  </servlet class>

  <load on startup> </load on startup>

  </servlet>

  <servlet mapping>

  <servlet name>CXFServlet</servlet name>

  <url pattern>/*</url pattern>

  </servlet mapping>

  </web app>

   .部署到tomcat服务器 输入//localhost: /<web app name>/ HelloWorld?wsdl 将显示这个web service的wsdl

  注意 如果web xml配置<servlet name>CXFServlet</servlet name>

  <url pattern>/ws/*</url pattern>

  则访问地址为 /ws/ //localhost: /<web app name>/ws/ HelloWorld?wsdl

   下面就开始创建一个客户端程序 访问这个web service 同样新建java project 并加入apache cxf \\lib所有包 创建与具体webservice技术无关的业务接口HelloWorld java

  public interface HelloWorld

  public String sayHello(String text);

  

   配置spring client xml

  <beans xmlns=

  xmlns:xsi= instance

  xmlns:jaxws=

  xsi:schemaLocation=

   beans xsd

   >

  <bean id= client class= test HelloWorld

  factory bean= clientFactory factory method= create />

  <bean id= clientFactory class= apache cxf jaxws JaxWsProxyFactoryBean >

  <property name= serviceClass value= test HelloWorld />

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

  <! 这个地方的地址一定要注意 正确的 >

  </bean>

  </beans>

   .测试

  import ntext ApplicationContext;

  import ntext support ClassPathXmlApplicationContext;

  import test HelloWorld;

  public class Test

  public static void main(String[] args)

  ApplicationContext ctx = new ClassPathXmlApplicationContext(

   spring client xml );

  HelloWorld client = (HelloWorld) ctx getBean( client );

  String result = client sayHello( 你好! );

  System out println(result);

  

cha138/Article/program/Java/ky/201311/28940

相关参考