使用Spring的ServletEndpointSupport实现WebService接口范例
一、环境配置 :在 eclipse 中配置引入相应的 Spring 框架( core/Remoting/Web )、 axis 包。
二、代码开发
1、 在 MyEclipse 中建立一个新的 J2EE 的 Web Project, 新建 java 包 test 。
2、 接口文件 HelloWorldRemote.java
package test;
//Spring 工程中要使用的接口文件 public interface HelloWorldRemote {
public String getMessage(String name); }
3、 接口实现文件 HelloWorldBean.java
package test;
//Spring 工程中要使用的接口实现文件
public class HelloWorldBean implements HelloWorldRemote {
private String helloStr; // Spring 中需要注入的字符串 public String getHelloStr() {
return helloStr; }
public void setHelloStr(String helloStr) {
this.helloStr = helloStr; }
// 实现接口中的方法
public String getMessage(String name) {
return helloStr + \ } }
4、 在 Spring 中对 Web Service 进行封装很简单,仅仅需要继承
org.springframework.remoting.jaxrpc.ServletEndpointSupport 类,实现里面的一些方法,包装一次,将其发布出来就可以。 HelloWorldWebService.java
package test;
import javax.xml.rpc.ServiceException;
import org.springframework.remoting.jaxrpc.ServletEndpointSupport; public
class
HelloWorldWebService
extends
ServletEndpointSupport
implements
HelloWorldRemote {
private HelloWorldRemote helloWorld; protected void onInit() throws ServiceException {
// 在 Spring 容器中获取 Bean 的实例
helloWorld = (HelloWorldRemote) getApplicationContext() .getBean(\ }
public String getMessage(String name) {
// 执行 Bean 中的相同的方法 return helloWorld.getMessage(name); } }
三、配置文件 (全部放在 /WEB-INF/ 目录下 )
1、 web.xml 为 web 加载 spring 和 axis 配置
org.springframework.web.context.ContextLoaderServlet
org.apache.axis.transport.http.AxisServlet
2、 applicationContext.xml 为 spring 的配置
3、 server-config.wsdd 为 axis 服务配置
xmlns:java=\ type=\ value=\
四、测试 客户端 TestWebServiceClient.java
package test;
import javax.xml.namespace.QName; import org.apache.axis.client.Call; import org.apache.axis.client.Service; public class TestWebServiceClient {
public static void main(String[] args) { try {
String wsdlUrl