日期:2014-05-17  浏览次数:20392 次

PHPRPC for Java Spring的例子
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"就得到类似下图的结果。如果现实,配置成功。


前两步成功后,我们看看该如何使用:

       // 调用时的写法

        PHPRPC_Client client = new PHPRPC_Client("http://localhost:8090/项目名称/phprpc/appservice.jspx");
        ApplicationServer appServer = (ApplicationServer) client.useService(ApplicationServer.class);
        appServer.register(username, password);   //要调用的方法

和不用spring的写法一摸一样,只是路径是配出来的。



  我亲自试了试,没有问题!!

  大家如有问题,可以一起讨论一下