服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

[webservices开发]集成spring

xfire可以很好的集成到spring中,spring的代码已经做了这方面的集成。

 

首先,我们先创建我们的web服务,采用接口和实现类的方式:

接口mathservice.java

package com.kuaff.xfire.samples;

 

public interface mathservice

{

    public long add(int p1, int p2);

}

 

 

实现类:

package com.kuaff.xfire.samples;

 

public class mathserviceimpl implements mathservice

{

    public long add(int p1, int p2)

    {

        return p1 + p2;

    }

}

 

 

meta-inf/xfire/service.xml文件可以省略了,因为web服务的定义在xfire-servlet.xml中可以找到。

 

下面要做的工具就是配置了。

web-inf文件夹下创建applicationcontext.xml文件,这是spring的配置文件,如果你使用其他的spring配置文件,可以将下面的bean添加到那个配置文件中:

<?xml version="1.0" encoding="utf-8"?>

<!doctype beans public "-//spring//dtd bean//en" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

    <bean id="mathbean" class="com.kuaff.xfire.samples.mathserviceimpl"/>

</beans>

 

 

定义了mathbean,这个bean就是我们的实现类,当然你也可以在这个文件中定义其他的需要spring管理的bean

 

web-inf文件夹下创建xfire-servlet.xml文件,根据spring规范,这个文件名起做xfire-servlet.xml,其中xfireweb.xml配置的dispatcherservlet的名称:

<?xml version="1.0" encoding="utf-8"?>

<!doctype beans public "-//spring//dtd bean//en" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

    <bean class="org.springframework.web.servlet.handler.simpleurlhandlermapping">

        <property name="urlmap">

            <map>

                <entry key="/mathservice">

                    <ref bean="math"/>

                </entry>

            </map>

        </property>

    </bean>

   

    <bean id="math" class="org.codehaus.xfire.spring.remoting.xfireexporter">

        <property name="servicefactory">

            <ref bean="xfire.servicefactory"/>

        </property>

        <property name="xfire">

            <ref bean="xfire"/>

        </property>

        <property name="servicebean">

            <ref bean="mathbean"/>

        </property>

        <property name="serviceclass">

            <value>com.kuaff.xfire.samples.mathservice</value>

        </property>

    </bean>

</beans>

 

这个文件的上半部分将mathservice这个urlmath这个bean联系在一起。下半部分定义了web服务的bean和服务接口。其中mathbean是我们在applicationcontext.xml中配置的那个bean

 

最后一步就是修改web.xml文件:

<?xml version="1.0" encoding="iso-8859-1"?>

 

<!doctype web-app

    public "-//sun microsystems, inc.//dtd web application 2.3//en"

    "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

    <context-param>

        <param-name>contextconfiglocation</param-name>

        <param-value>/web-inf/applicationcontext.xml

        classpath:org/codehaus/xfire/spring/xfire.xml</param-value>

    </context-param>

 

    <context-param>

        <param-name>log4jconfiglocation</param-name>

        <param-value>/web-inf/log4j.properties</param-value>

    </context-param>

 

    <listener>

        <listener-class>org.springframework.web.util.log4jconfiglistener</listener-class>

    </listener>

 

    <listener>

        <listener-class>org.springframework.web.context.contextloaderlistener</listener-class>

    </listener>

 

    <servlet>

        <servlet-name>xfire</servlet-name>

        <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>

    </servlet>

 

    <servlet-mapping>

        <servlet-name>xfire</servlet-name>

        <url-pattern>/*</url-pattern>

    </servlet-mapping>

</web-app>

 

[webservices开发]集成spring

需要注意这个文件的三个部分:

 

1.      在定义contextconfiglocation参数时一定要加上classpath:org/codehaus/xfire/spring/xfire.xml

2.      定义listener: org.springframework.web.context.contextloaderlistener

3.      定义dispatcherservlet: xfire

 

这样,你就可以访问http://localhost:8080/xfire/mathservice来调用这个web服务,也可以通过网址http://localhost:8080/xfire/mathservice?wsdl来查看wsdl文档。

扫描关注微信公众号