---
title: "PHPRPC for Java Spring的例子"
author: "Java_KAbanban"
date: "2011-06-01T00:53:45Z"
tags: []
categories: ["编程语言"]
slug: "phprpc-for-java-spring"
draft: false
lastmod: "2011-06-01T00:53:45Z"
id: 1066804
url: "https://java-kabanban.iteye.com/blog/1066804"
---

1.需要在web.xml中配置

      web.xml文件：

      <!--PHPRPC for JAVASPRING -->
         <servlet>
              <servlet-name>phprpc</servlet-name>
             <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
             <init-param>

                    <!--配置spring配置文件的路径-->
                   <param-name>contextConfigLocation</param-name>
                  <param-value>WEB-INF/config/phprpc-servlet-front-action.xml</param-value> 
            </init-param>
            <load-on-startup>2</load-on-startup>
       </servlet>
       <servlet-mapping>
           <servlet-name>phprpc</servlet-name>
           <url-pattern>/phprpc/*</url-pattern>
      </servlet-mapping>

2. 配置spring

      我的是配置文件在WEB-INF/config/phprpc-servlet-front-action.xml:

     相关内容为：

      <!-- phprpc for javaspring -->

     <!--appservice中要注入的内容，需要set和get方法。同时此处用property配置好。不讲详细了-->
     <bean id="appservice" class="com.phprpc.sso.service.ApplicationService">
           <property name="userMng" ref="userMng"></property>
     </bean>

    <!--这里是关键。"/appservice.jspx"表示要访问的路径为http://localhost:8080/项目名/phprpc/appservice.jspx 

    -->
    <bean name="/appservice.jspx" class="org.phprpc.spring.remoting.PHPRPC_Exporter">
         <!-- service属性是 -->
         <property name="service" ref="appservice" /><!--具体操作，实现类，即上边配置的appservicebean-->
          <!--配置接口，即上边配置的appservicebean实现的接口-->

         <property name="serviceInterface" value="com.phprpc.sso.server.Application" />
     </bean>

第二步配置好后，在地址栏中键入"http://localhost:8080/项目名/phprpc/appservice.jspx"就得到类似下图的结果。如果现实，配置成功。

 ![](http://hiphotos.baidu.com/marlon_xiaoyu/pic/item/9c0630179bb1c57fdc54019a.jpg)
前两步成功后，我们看看该如何使用：

       // 调用时的写法

        PHPRPC_Client client = new PHPRPC_Client("http://localhost:8090/项目名称/phprpc/appservice.jspx");
        ApplicationServer appServer = (ApplicationServer) client.useService(ApplicationServer.class);
        appServer.register(username, password);   //要调用的方法

和不用spring的写法一摸一样，只是路径是配出来的。

 

  我亲自试了试，没有问题！！

  大家如有问题，可以一起讨论一下 :lol: