歡迎您光臨本站 註冊首頁

CXF與spring集成

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0

  1. 新建web project ,並加入apache-cxf-2.0.7lib所有包,編寫要發布的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 服務

  2. 在spring-cxf.xml配置發布的web service

  <?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"

  xsi:schemaLocation="

  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

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

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

  3. 配置web.xml文件:

  <?xml version="1.0" encoding="UTF-8"?>

  <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"

  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <context-param>

  <param-name>contextConfigLocation</param-name>

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

  </context-param>

  <listener>

  <listener-class>

  org.springframework.web.context.ContextLoaderListener

  </listener-class>

  </listener>

  <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>/*</url-pattern>

  </servlet-mapping>

  </web-app>

  4.部署到tomcat伺服器,輸入:http://localhost:8080/<web-app-name>/ HelloWorld?wsdl,將顯示這個web service的wsdl.

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

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

  則訪問地址為:/ws/">http://localhost:8080/<web-app-name>/ws/ HelloWorld?wsdl

  5. 下面就開始創建一個客戶端程序,訪問這個web service, 同樣新建java project ,並加入apache-cxf-2.0.7lib所有包. 創建與具體webservice技術無關的業務介面HelloWorld.java

  public interface HelloWorld {

  public String sayHello(String text);

  }

  6.配置spring-client.xml

  <beans xmlns="http://www.springframework.org/schema/beans"

  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xmlns:jaxws="http://cxf.apache.org/jaxws"

  xsi:schemaLocation="

  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

  http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">

  <bean id="client" class="test.HelloWorld"

  factory-bean="clientFactory" factory-method="create"/>

  <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">

  <property name="serviceClass" value="test.HelloWorld"/>

  <property name="address" value="http://localhost:9000/helloWorld"/>

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

  </bean>

  </beans>

  7.測試:

  import org.springframework.context.ApplicationContext;

  import org.springframework.context.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);

  }

  }


[火星人 ] CXF與spring集成已經有709次圍觀

http://coctec.com/docs/java/show-post-60234.html