How-Tos_Writing a service with spring

来源:互联网 发布:农村淘宝要投资多少钱 编辑:程序博客网 时间:2024/06/06 06:32

使用spring构件第一个Web服务,项目名java_first_spring_support

构件项目

编写服务

接口:

<span style="font-size:18px;">import javax.jws.WebService; @WebServicepublic interface HelloWorld {    String sayHi(String text);}</span>
接口实现:

<span style="font-size:18px;">import javax.jws.WebService; @WebService(endpointInterface = "demo.spring.service.HelloWorld")public class HelloWorldImpl implements HelloWorld {     public String sayHi(String text) {        System.out.println("sayHi called");        return "Hello " + text;    }}</span>

实现类的@ webservice注释让CXF知道使用哪个接口在创建WSDL。

声明服务的beans

<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-servlet.xml"/>    <jaxws:endpoint id="helloWorld" implementor="demo.spring.service.HelloWorldImpl" address="/HelloWorld"/></beans>
配置servlet
例子:

<web-app ...>...   <context-param>      <param-name>contextConfigLocation</param-name>      <param-value>WEB-INF/beans.xml</param-value>   </context-param>    <listener>      <listener-class>         org.springframework.web.context.ContextLoaderListener      </listener-class>   </listener></web-app>

创建客户端(easy way

客户端bean

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:jaxws="http://cxf.apache.org/jaxws"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">     <jaxws:client id="helloClient"                  serviceClass="demo.spring.HelloWorld"                  address="http://localhost:9002/HelloWorld" /></beans>
创建客户端(More Manual Way)

使用JaxWsProxyFactory从服务接口创建一个客户端

客户端bean:

<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/schema/jaxws.xsd">    <bean id="client" class="demo.spring.service.HelloWorld" factory-bean="clientFactory" factory-method="create"/>    <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">        <property name="serviceClass" value="demo.spring.service.HelloWorld"/>        <property name="address" value="http://localhost:9002/services/HelloWorld"/>    </bean></beans>
客户端代码:
<pre name="code" class="html">ApplicationContext context = ...; // your Spring ApplicationContextHelloWorld client = (HelloWorld) context.getBean("client");

0 0
原创粉丝点击