`

spring httpInvoke接口

阅读更多

 

有几个主要步骤:

  接口类与实现类省略:  

     接口:com.base.HttpinvokeInterface.java  接口方法:test()  

     实现类:com.impl.HttpinvokeService

1.Server端web.xml配置信息: 

a:一种DispatcherServlet方式,需要在servlet-name配置的spring.xml文件名必须和该名称一致

<servlet>

       <servlet-name>httpinvoke(名称必须与配置横杠前面文件名一样如:httpinvoke-servlet.xml,配置与web.xml在同一个目录)</servlet-name>

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

</servlet>

<servlet-mapping>

      <servlet-name>httpinvoke</servlet-name>

      <url-pattern>/ httpinvoke/*(调用定义接口地址入口)</url-pattern>

</servlet-mapping>

b:另外一种HttpRequestHandlerServlet方式,无需保持spring配置文件名和当前参数一致。确保servlet-name中和spring的bean name一致

<servlet>

      <servlet-name>serviceBeanName</servlet-name>

      <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>

</servlet> 

<servlet-mapping>

      <servlet-name>serviceBeanName</servlet-name>

      <url-pattern>/ httpinvoke/serviceBeanName</url-pattern>

</servlet-mapping>

 

2.Server端Spring配置 httpinvoke-servlet.xml(1.a的配置对应的文件名,1.b 文件名无限制):

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

    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"

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

    <beans>

 

     

     <bean name="/httpinvokeService"   //1.a方式配置

     <bean name="serviceBeanName"     //1.b方式配置

       class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">                       

        <property name="service">

            <ref bean=" httpinvokeService"/>

        </property>

        <property name="serviceInterface">

            <value>com.base.HttpinvokeInterface</value>

        </property>

    </bean>

    <baan id="httpinvokeService" class="com.impl.HttpinvokeService">

</beans>

 

3.Client端Spring配置:

<?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="httpinvokeService"

class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">

<property name="serviceUrl" value="http://localhost:9111/CMS/ httpinvoke/ httpinvokeServiceService" />

<!-- 服务器IP地址:端口号/项目名称/(Server端web.xml中配置的<url-pattern>入口)/(服务器端配置的bean的name属性<bean name="/httpinvokeService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">......</bean>)-->     

<property name="serviceInterface" value="com.base. HttpinvokeInterface"/>

</bean>

</beans>

 

4.Client端测试:

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

import com.base.HttpinvokeInterface

 

public class Test {

 

/**

* @param args

*/

public static void main(String[] args) {

           ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Spring远程调用客户端配置.xml");   //读取配置文件

            HttpinvokeInterface service = (HttpinvokeInterface)applicationContext.getBean("httpinvokeService");   //通过接口得到服务器信息

           service.test();   //调用服务器端的方法

           System.out.println("远程调用,测试成功!");

}

 

}

 
 
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics