spring httpInvoke接口

 

有几个主要步骤:

  接口类与实现类省略:  

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

     实现类:com.impl.HttpinvokeService

1.Server端web.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>

 

2.Server端Spring配置 httpinvoke-servlet.xml:

   <?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"          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("远程调用,测试成功!");

}

 

}

 
 

猜你喜欢

转载自yrandy.iteye.com/blog/1090147