- 前言
- 第一部分 核心实现
- 第 1 章 Spring 整体架构和环境搭建
- 第 2 章 容器的基本实现
- 第 3 章 默认标签的解析
- 第 4 章 自定义标签的解析
- 第 5 章 bean 的加载
- 第 6 章 容器的功能扩展
- 第 7 章 AOP
- 第二部分 企业应用
- 第 8 章 数据库连接 JDBC
- 第 9 章 整合 MyBatis
- 第 10 章 事务
- 第 11 章 SpringMVC
- 第 12 章 远程服务
- 第 13 章 Spring 消息
12.2.1 使用示例
(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 包并加入到客户端的服务器上。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论