返回介绍

12.2.1 使用示例

发布于 2025-04-22 22:09:18 字数 3008 浏览 0 评论 0 收藏

(1)创建对外接口。

public interface HttpInvokerTestI {

  public String getTestPo(String desp);

}

(2)创建接口实现类。

public class HttpInvokertestImpl implements HttpInvokerTestI {

 @Override

  public String getTestPo(String desp) {

   return "getTestPo " + desp;

 }

}

(3)创建服务端配置文件 applicationContext-server.xml。

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

<beans xmlns="http://www.Springframework.org/schema/beans"

 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 xsi:schemaLocation="

http://www.Springframework.org/schema/beans

http://www.Springframework.org/schema/beans/Spring-beans-3.0.xsd ">

  <bean name="httpinvokertest" class="test.HttpInvokertestImpl" />

</beans>

(4)在 WEB-INF 下创建 remote-servlet.xml。

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

<beans xmlns="http://www.Springframework.org/schema/beans"

 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 xsi:schemaLocation="

http://www.Springframework.org/schema/beans

http://www.Springframework.org/schema/beans/Spring-beans-3.0.xsd ">

  <bean name="/hit"

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

   <property name="service" ref="httpinvokertest" />

   <property name="serviceInterface" value="test.HttpInvokerTestI" />

 </bean>

</beans>

至此,服务端的 httpInvoker 服务已经搭建完了,启动 Web 工程后就可以使用我们搭建的 HttpInvoker 服务了。以上代码实现了将远程传入的字符串参数处理加入“getTestPo”前缀的功能。服务端搭建完基于 Web 服务的 HttpInvoker 后,客户端还需要使用一定的配置才能进行远程调用。

(5)创建测试端配置 client.xml。

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

<beans xmlns="http://www.Springframework.org/schema/beans"

 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 xsi:schemaLocation="

http://www.Springframework.org/schema/beans

http://www.Springframework.org/schema/beans/Spring-beans-3.0.xsd ">

  <bean id="remoteService"

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

   <property name="serviceUrl" value="http://localhost:8080/httpinvokertest/

   remoting/hit" />

   <property name="serviceInterface" value="test.HttpInvokerTestI" />

 </bean>

</beans>

(6)创建测试类。

public class Test {

  public static void main(String[] args) {

   ApplicationContext context = new ClassPathXmlApplicationContext ("classpath:

  client.xml");

   HttpInvokerTestI httpInvokerTestI = (HttpInvokerTestI) context.getBean

  ("remoteService");

  System.out.println(httpInvokerTestI.getTestPo("dddd"));

 }

}

运行测试类,你会看到打印结果:

getTestPo dddd

dddd 是我们传入的参数,而 getTestPo 则是在服务端添加的字符串。当然,上面的服务搭建与测试过程中都是在一台机器上进行的,如果需要在不同机器上进行测试,还需要读者对服务端的相关接口打成 JAR 包并加入到客户端的服务器上。

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。