XFire构建服务端service的两种方式,一是用xfire构建,二是和spring集成构建。
一,xifre构建,确保把xfire的jar包导入到工程中或classpath。
1,service的接口类
- package cn.cjw.services.exchange;
- public interface ExchangeService {
- public double RMB2Dollar(double RMB);
- }
2,service的实现类
- package cn.cjw.services.exchange.impl;
- import cn.cjw.services.exchange.ExchangeService;
- public class ExchangeServiceImpl implements ExchangeService {
- private double EXCHANGERATE = 6.9;
- /**
- * @return the eXCHANGERATE
- */
- public double getEXCHANGERATE() {
- return EXCHANGERATE;
- }
- /**
- * @param exchangerate the eXCHANGERATE to set
- */
- public void setEXCHANGERATE(double exchangerate) {
- EXCHANGERATE = exchangerate;
- }
- public double RMB2Dollar(double RMB) {
- // TODO Auto-generated method stub
- return RMB*(1/EXCHANGERATE);
- }
- }
3,在你的src文件夹下新建一个META-INF/xfire文件夹,同时新建一个services.xml文件。 services.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://xfire.codehaus.org/config/1.0">
- <service>
- <name>ExchangeService</name>
- <namespace>http://www.chinajavaworld.com/ExchangeService</namespace>
- <serviceClass>cn.cjw.services.exchange.ExchangeService</serviceClass>
- <implementationClass>cn.cjw.services.exchange.impl.ExchangeServiceImpl</implementationClass>
- </service>
- </beans>
4,配置你的项目中的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">
- <servlet>
- <servlet-name>XFireServlet</servlet-name>
- <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>XFireServlet</servlet-name>
- <url-pattern>/servlet/XFireServlet/*</url-pattern>
- </servlet-mapping>
- <servlet-mapping>
- <servlet-name>XFireServlet</servlet-name>
- <url-pattern>/services/*</url-pattern>
- </servlet-mapping>
- </web-app>
到这里,把以上的工程导入到tomcat的webapps下就可以使用了。这和axis比起来,省略了打包的过程。部署到tomcat后启动,在浏览器输入http://localhost:port/[yourProjectName]/services/ExchangeService?wsdl,注意:这里的yourProjectName就是你建立的工程名字。如果ok,那么说明你的service已经可以部署ok了。
二,集成spring的部署,spring和xfire的jar包需要导入到工程中。 1,建立service的接口类和实现类,代码和以上相同。 2,spring的applicationContext.xml文件
- <?xml version="1.0" encoding="UTF-8"?>
- <beans
- xmlns="http://www.springframework.org/schema/beans"
- 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-2.0.xsd">
- <import resource="classpath:org/codehaus/xfire/spring/xfire.xml"/>
- <bean id="ExchangeService" class="org.codehaus.xfire.spring.remoting.XFireExporter">
- <property name="serviceFactory" ref="xfire.serviceFactory"></property>
- <property name="xfire" ref="xfire"></property>
- <property name="serviceBean" ref="ExchangeServiceImpl"></property>
- <property name="serviceClass" value="cn.cjw.services.exchange.ExchangeService"></property>
- <property name="name" value="ExchangeService"></property>
- </bean>
- <bean id="ExchangeServiceImpl" class="cn.cjw.services.exchange.impl.ExchangeServiceImpl"></bean>
- </beans>
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>/WEB-INF/applicationContext.xml</param-value>
- </context-param>
- <listener>
- <listener-class>
- org.springframework.web.context.ContextLoaderListener
- </listener-class>
- </listener>
- <servlet>
- <servlet-name>xfireSpringServlet</servlet-name>
- <servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>xfireSpringServlet</servlet-name>
- <url-pattern>/services/*</url-pattern>
- </servlet-mapping>
- </web-app>
把该工程部署到tomcat下,启动tomcat,在浏览器中输入:http://localhost:port/[yourProjectName]/services/ExchangeService?wsdl,如果ok,说明这个service已经部署ok了。相比前一个,这第二种方式可以说简便,而且能够使用spring的所有特性,如IOC和AOP。