How-Tos_Writing a service with spring
2015-11-25 13:53
453 查看
使用spring构件第一个Web服务,项目名java_first_spring_support
构件项目
编写服务
接口:
实现类的@ webservice注释让CXF知道使用哪个接口在创建WSDL。
声明服务的beans
例子:
创建客户端(easy way)
客户端bean
使用JaxWsProxyFactory从服务接口创建一个客户端
客户端bean:
构件项目
编写服务
接口:
<span style="font-size:18px;">import javax.jws.WebService; @WebService public 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.xsd http://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 ApplicationContext HelloWorld client = (HelloWorld) context.getBean("client");
相关文章推荐
- eclipse编码格式设置教程、如何为eclipse设置编码格式?
- java acm hdu 2083
- Spring Boot整合Activiti,查看流程图出现中文乱码问题
- java 多线程归纳整理
- leetcode_java_twosum
- 分享史上java最牛逼,最简短的代码
- java 访问权限使用总结
- Java压缩文件
- Struts之文件上传
- java 反射api的功能详解
- java hdu 2082
- Java反射技术概述
- Java――JavaWeb项目开发的总结
- 如何在已安装有jdk的环境下安装高版本jdk
- Java正则表达式进阶(二):简单查询域名的Whois信息
- Java毕业设计之简单易懂计算器+源代码+注释
- java.lang.OutOfMemoryError: GC overhead limit exceeded解决
- AES加密/解密 IOS 端,JAVA端 通用,不限明文字符长度
- Java基础之详解Thread与Runnable
- Java正则表达式进阶(一):写出常用的正则模式