本文只是介绍XFire+Spring使用注解的集成,版本为Xfire 1.2.6+Spring 3.0.5,测试环境为Tomcat6.0。
1.首先建一个Web工程,引入相应的jar包,这一步不再多说。
2.修改web.xml,加入以下代码:
[html] <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>xfireServlet</servlet-name> <servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>xfireServlet</servlet-name> <url-pattern>/service/*</url-pattern> </servlet-mapping>
[html] <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>xfireServlet</servlet-name> <servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>xfireServlet</servlet-name> <url-pattern>/service/*</url-pattern> </servlet-mapping>
3.在classpath下加入Spring配置文件applicationContext.xml,加入以下代码:
[html] <context:component-scan base-package="my.webservice" /> <!-- XFire start --> <import resource="classpath:org/codehaus/xfire/spring/xfire.xml" /> <bean id="webAnnotations" class="org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations" /> <bean id="jsr181HandlerMapping" class="org.codehaus.xfire.spring.remoting.Jsr181HandlerMapping"> <property name="xfire" ref="xfire" /> <property name="webAnnotations" ref="webAnnotations" /> </bean> <!-- XFire end -->
[html] <context:component-scan base-package="my.webservice" /> <!-- XFire start --> <import resource="classpath:org/codehaus/xfire/spring/xfire.xml" /> <bean id="webAnnotations" class="org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations" /> <bean id="jsr181HandlerMapping" class="org.codehaus.xfire.spring.remoting.Jsr181HandlerMapping"> <property name="xfire" ref="xfire" /> <property name="webAnnotations" ref="webAnnotations" /> </bean> <!-- XFire end -->
4.定义WebService接口,添加相应注解:
[java] @WebService public interface IBookService { @WebMethod public Book getBook(); }
[java] @WebService public interface IBookService { @WebMethod public Book getBook(); }
5.接口实现类,注解中serviceName定义发布的服务名,endpointInterface定义实现的接口:
[java] @Component @WebService(serviceName="BookService", endpointInterface = "my.webservice.IBookService") public class BookServiceImpl implements IBookService { @Override public Book getBook() { Book b = new Book(1, "Java核心思想", 100); System.out.println(">>>>>>Server: " + b); return b; } }
[java] @Component @WebService(serviceName="BookService", endpointInterface = "my.webservice.IBookService") public class BookServiceImpl implements IBookService { @Override public Book getBook() { Book b = new Book(1, "Java核心思想", 100); System.out.println(">>>>>>Server: " + b); return b; } }
6.以上便是服务端的配置及实现,是不是很简单。把工程发布到Tomcat并启动,在浏览器中输入:http://127.0.0.1:8080/XFireTest/service/BookService?wsdl(BookService为上面serviceName定义的名称),如果浏览器中显示BookService相关xml信息,则表示WebService发布成功。
7.客户端调用服务端方法:
[java]
@Test public void testBookService() { Service serviceModel = new ObjectServiceFactory().create(IBookService.class); String url = "http://127.0.0.1:8080/XFireTest/service/BookService"; IBookService service = null; try { service = (IBookService) new XFireProxyFactory().create(serviceModel, url); Book b = service.getBook(); System.out.println(">>>>>>>>Client: " + b); } catch (Exception e) { e.printStackTrace(); } }
下面来讲客户端调用。===================
1. 加入跟server端一样的library。参考上面的第1点
2. 将serviceClass对应的接口加入客户端中。
3. 书写调用代码: PS:代码太简单,不另外注释了。
private String url = "http://10.4.1.16:8080/eccn/services/exCommService";
public String checkLicenseByOB(String obno) throws Exception{
Service serviceModel = new ObjectServiceFactory().create(IExCommService.class);
IExCommService service = (IExCommService)
new XFireProxyFactory().create(serviceModel, url);
return service.checkLicenseByOB(obno);
}
在生成接口对应的service后,用户就可以像调用本地方法一样调用远程的方法了。
转载自:http://blog.csdn.net/xinying0424/article/details/8112113