Spring实现远程调用

前段时间为了完成项目中的远程调用功能,查了很多资料,学习了Webservice远程调用和Spring远程调用,现在将Spring远程调用的方法总结如下,希望对有需要的人能够提供一点点帮助。

远程调用分为两个工程:客户端和服务器端,每个工程都必须添加Spring 的Core,AOP,Remoting,Web 4个jar包。
服务器端如下:
src下有一个包,包下有一个接口和一个实现了该接口的类
接口和实现类内容:
public interface HelloService {
String getPerson(String name);
}

public class HelloServiceImpl implements HelloService {

public String getPerson(String name) {
// TODO Auto-generated method stub

return "Hello "+name;
}

}

包结构图如下:




WebRoot下的WEB-INF下有两个配置文件 web.xml和server_config.xml
web.xml的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<servlet>
<servlet-name>remoteservice</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/server_config.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>remoteservice</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>


<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


server_config.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-2.5.xsd">

<!--<bean id="service" class="hello.HelloService"></bean>
--><bean id="serviceImpl" class="hello.HelloServiceImpl"></bean>

<bean name="/service.do"
class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="serviceInterface" value="hello.HelloService"></property>
<property name="service" ref="serviceImpl"></property>
</bean>

</beans>


客户端如下:
src下有一个包和一个配置文件client.xml,包内有一个和服务器端一致的接口和一个测试类
包结构图如下:




测试类的内容如下:
public class TestService {

/**
* @param args
*/
public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("client_config.xml");
        HelloService service = (HelloService)context.getBean("serviceProxy");
        String str = service.getPerson("zhangrongrong");
        System.out.println(str);
}

}

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-2.5.xsd">

<bean id="serviceProxy"
class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl"
value="http://127.0.0.1:8080/spring_server/service.do">
</property>
<property name="serviceInterface"
value="chello.HelloService">
</property>
</bean>

</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值