`
zhuyuanxiang
  • 浏览: 127058 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

webservice-之使用xfire+spring开发(转自勇哥的BLOG)

阅读更多

1 、想要发布成webservice的类,必须实现接口(Spring2.0,Xfile1.2)<!----><o:p></o:p>

2 3个配置文件(后面详细说)<o:p></o:p>

----------------------------------------------<o:p></o:p>

HelloRemote.java<o:p></o:p>

package test;<o:p></o:p>

public interface HelloRemote {<o:p></o:p>

    public String hello();   <o:p></o:p>

    public String helloTo(String name);   <o:p></o:p>

    public String getJsonData(String pageIndex,String pageSize); <o:p></o:p>

}<o:p></o:p>

----------------------------------------------<o:p></o:p>

HelloBean.java<o:p></o:p>

package test;<o:p></o:p>

import javax.servlet.http.HttpServletRequest;<o:p></o:p>

import javax.servlet.http.HttpSession;<o:p></o:p>

import org.codehaus.xfire.transport.http.XFireServletController;<o:p></o:p>

public class HelloBean implements HelloRemote <o:p></o:p>

{<o:p></o:p>

    public String hello() <o:p></o:p>

    {<o:p></o:p>

        return "hello";<o:p></o:p>

    }<o:p></o:p>

    public String helloTo(String name) <o:p></o:p>

    {<o:p></o:p>

        return " hello " + name + "!";<o:p></o:p>

    }<o:p></o:p>

    private String example() <o:p></o:p>

    {<o:p></o:p>

        // 使用session<o:p></o:p>

        HttpServletRequest request = XFireServletController.getRequest();<o:p></o:p>

        HttpSession session = request.getSession();     <o:p></o:p>

        return " 请求地址:"+request.getLocalAddr()<o:p></o:p>

+", 会话创建时间:"+session.getCreationTime();<o:p></o:p>

    }<o:p></o:p>

    public String getJsonData(String pageIndex, String pageSize) <o:p></o:p>

    {<o:p></o:p>

        String rtnValue = "";<o:p></o:p>

        rtnValue = example();<o:p></o:p>

        return rtnValue;<o:p></o:p>

    }<o:p></o:p>

}<o:p></o:p>

----------------------------------------------<o:p></o:p>

web.xml<o:p></o:p>

<?xml version="1.0" encoding="UTF-8"?><o:p></o:p>

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"<o:p></o:p>

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<o:p></o:p>

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee <o:p></o:p>

    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><o:p></o:p>

    <!-- 配置文件路径开始,指定加载的配置文件 --><o:p></o:p>

    <context-param><o:p></o:p>

        <param-name>contextConfigLocation</param-name><o:p></o:p>

        <param-value><o:p></o:p>

            /WEB-INF/classes/applicationContext*.xml<o:p></o:p>

            classpath:org/codehaus/xfire/spring/xfire.xml<o:p></o:p>

        </param-value><o:p></o:p>

    </context-param><o:p></o:p>

    <!-- 启动时加载SpringContextServlet --><o:p></o:p>

    <listener><o:p></o:p>

        <listener-class><o:p></o:p>

            org.springframework.web.context.ContextLoaderListener<o:p></o:p>

        </listener-class><o:p></o:p>

    </listener><o:p></o:p>

    <listener><o:p></o:p>

        <listener-class><o:p></o:p>

            org.springframework.web.util.IntrospectorCleanupListener<o:p></o:p>

        </listener-class><o:p></o:p>

    </listener><o:p></o:p>

    <!-- XFire 配置 --><o:p></o:p>

    <servlet><o:p></o:p>

        <servlet-name>xfire</servlet-name><o:p></o:p>

        <servlet-class><o:p></o:p>

            org.springframework.web.servlet.DispatcherServlet<o:p></o:p>

        </servlet-class><o:p></o:p>

    </servlet><o:p></o:p>

    <servlet-mapping><o:p></o:p>

        <servlet-name>xfire</servlet-name><o:p></o:p>

        <url-pattern>*.ws</url-pattern><o:p></o:p>

    </servlet-mapping><o:p></o:p>

<o:p> </o:p>

    <welcome-file-list><o:p></o:p>

        <welcome-file>index.jsp</welcome-file><o:p></o:p>

    </welcome-file-list><o:p></o:p>

</web-app><o:p></o:p>

这里注意:classpath:org/codehaus/xfire/spring/xfire.xml必须要写进去。<o:p></o:p>

----------------------------------------------<o:p></o:p>

xfire-servlet.xml 新建这个文件,并且和web.xml放在同一个文件夹。<o:p></o:p>

注意: 名称和位置都不能变。<o:p></o:p>

<?xml version="1.0" encoding="UTF-8"?><o:p></o:p>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" <o:p></o:p>

"http://www.springframework.org/dtd/spring-beans.dtd"><o:p></o:p>

<beans><o:p></o:p>

    <bean       class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"><o:p></o:p>

        <property name="urlMap"><o:p></o:p>

            <map><o:p></o:p>

                <entry key="/myHelloService.ws"><!-- 暴露的服务名称 --><o:p></o:p>

                    <ref bean="myTest" /><o:p></o:p>

                </entry><o:p></o:p>

            </map><o:p></o:p>

        </property><o:p></o:p>

    </bean><o:p></o:p>

    <bean id="myTest" parent="webService"<o:p></o:p>

        class="org.codehaus.xfire.spring.remoting.XFireExporter"><o:p></o:p>

        <!-- 定义实现类(指向applicationContext-webService.xml) --><o:p></o:p>

        <property name="serviceBean"><o:p></o:p>

            <ref bean="myHelloBean" /><o:p></o:p>

        </property><o:p></o:p>

        <!-- 定义接口类 --><o:p></o:p>

        <property name="serviceClass"><o:p></o:p>

            <value>test.HelloRemote</value><o:p></o:p>

        </property><o:p></o:p>

    </bean><o:p></o:p>

    <!-- webService 基本配置 --><o:p></o:p>

    <bean id="webService"<o:p></o:p>

        class="org.codehaus.xfire.spring.remoting.XFireExporter"<o:p></o:p>

        abstract="true"><o:p></o:p>

        <property name="serviceFactory"><o:p></o:p>

            <ref bean="xfire.serviceFactory" /><o:p></o:p>

        </property><o:p></o:p>

        <property name="xfire"><o:p></o:p>

            <ref bean="xfire" /><o:p></o:p>

        </property><o:p></o:p>

    </bean><o:p></o:p>

</beans><o:p></o:p>

----------------------------------------------<o:p></o:p>

spring 的配置文件 applicationContext-webService.xml<o:p></o:p>

<?xml version="1.0" encoding="UTF-8"?>   <o:p></o:p>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" <o:p></o:p>

"http://www.springframework.org/dtd/spring-beans.dtd">     <o:p></o:p>

<beans>   <o:p></o:p>

    <bean id="myHelloBean" class="test.HelloBean"></bean>   <o:p></o:p>

</beans><o:p></o:p>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics