分享

Spring HTTP Invoker例子

 老年图书馆 2011-12-04

Spring HTTP Invoker例子

标签: #Java编程 |  发布时间:2011-06-19

Spring HTTP Invoker例子,Spring HTTP Invoker是spring框架中的一个远程调用模型,执行基于HTTP的远程调用,也就是说,可以通过防火墙,并使用java的序列化机制在网络间传 递对象。客户端可以很轻松的像调用本地对象一样调用远程服务器上的对象,要注意的一点是,服务端、客户端都是使用Spring框架。下面通过一个例子,来 讲解Spring HTTP Invoker的使用,这里使用的是Spring3.0框架。

整体流程如下:
1.服务器端:通过Spring HTTP Invoker,将服务接口的某个实现类提供为远程服务。
2.客户端:通过Spring HTTP Invoker代理,向服务端发送请求,远程调用服务端接口。

注意事项:由于是通过网络传输,所以服务端、客户端的POJO类,都要实现Serializable接口,进行序列化、反序列化。

先看看服务端的配置:
1.先提供一个服务接口
01.package spring.invoker.service;
02. 
03.import spring.invoker.domain.UserInfo;
04. 
05./**
06.* spring http invoker例子
07.* @author steven
08.*
09.*/
10.public interface UserService {
11./**
12.* 根据用户名,获取用户信息
13.* @param userName  用户名
14.* @return   返回用户信息
15.*/
16.public UserInfo getUserInfobyName(String userName);
17. 
18. 
19. 
20./**
21.* 根据用户名,获取用户邮箱
22.* @param userName  用户名
23.* @return   返回用户邮箱
24.*/
25.public String getUserEmailbyName(String userName);
26.}

2.再实现这个服务接口,像平时写service一样,这里只是一个简单的例子,可以在这里调用自己的DAO,查询数据库。
01.package spring.invoker.service.impl;
02. 
03.import spring.invoker.domain.UserInfo;
04.import spring.invoker.service.UserService;
05. 
06./**
07.* spring http invoker例子
08.* @author steven
09.*
10.*/
11.public class UserServiceImpl implements UserService{
12. 
13. 
14./**
15.* 根据用户名,获取用户信息
16.* @param userName  用户名
17.* @return   返回用户信息
18.*/
19.public UserInfo getUserInfobyName(String userName){
20.UserInfo userInfo = new UserInfo();
21.userInfo.setUserName(userName);
22.userInfo.setEmail("xxx@site.com");
23. 
24.return userInfo;
25.}
26. 
27. 
28. 
29./**
30.* 根据用户名,获取用户邮箱
31.* @param userName  用户名
32.* @return   返回用户邮箱
33.*/
34.public String getUserEmailbyName(String userName){
35.return userName + "的邮箱地址为:xxx.site.com";
36.}
37.}

3.接下来,就是POJO类了,这个类一定要实现Serializable接口,并指定一个serialVersionUID。
01.package spring.invoker.domain;
02. 
03.import java.io.Serializable;
04. 
05./**
06.* 用户基本信息
07.* @author steven
08.*
09.*/
10.public class UserInfo implements Serializable {
11.private static final long serialVersionUID = 1L;
12.private String userName;
13.private String email;
14. 
15.public String getUserName() {
16.return userName;
17.}
18.public void setUserName(String userName) {
19.this.userName = userName;
20.}
21.public String getEmail() {
22.return email;
23.}
24.public void setEmail(String email) {
25.this.email = email;
26.}
27. 
28.}


4.将接口声明为HTTP Invoker服务
01.<?xml version="1.0" encoding="UTF-8"?>
02.<beans xmlns="http://www./schema/beans"
03.xmlns:xsi="http://www./2001/XMLSchema-instance"
04.xmlns:p="http://www./schema/p"
05.xmlns:context="http://www./schema/context"
06.xsi:schemaLocation="
07.http://www./schema/beans
08.http://www./schema/beans/spring-beans-3.0.xsd
09.http://www./schema/context
10.http://www./schema/context/spring-context-3.0.xsd">
11. 
12.<!-- 交给Spring管理,bean的名称是:userService -->
13.<bean id="userService" class="spring.invoker.service.impl.UserServiceImpl"/>
14. 
15.<!-- 这个配置,就是把userService接口,提供给远程调用 -->
16.<bean id="httpService"
17.class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
18.<property name="service">
19.<ref bean="userService" />
20.</property>
21.<property name="serviceInterface"
22.value="spring.invoker.service.UserService">
23.</property>
24.</bean>
25. 
26.<!-- 远程服务的URL -->
27.<bean
28.class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
29.<property name="mappings">
30.<props>
31.<prop key="/test">httpService</prop>
32.</props>
33.</property>
34.</bean>
35. 
36.</beans>

5.WEB-INF/web.xml:配置spring监听及DispatcherServlet
01.<?xml version="1.0" encoding="UTF-8"?>
02.<web-app xmlns:xsi="http://www./2001/XMLSchema-instance"
03.xmlns="http://java./xml/ns/javaee" xmlns:web="http://java./xml/ns/javaee/web-app_2_5.xsd"
04.xsi:schemaLocation="http://java./xml/ns/javaee http://java./xml/ns/javaee/web-app_2_5.xsd"
05.id="WebApp_ID" version="2.5">
06.<display-name>Spring3.0 Test Demo</display-name>
07.<context-param>
08.<param-name>contextConfigLocation</param-name>
09.<param-value>
10./WEB-INF/dispatcher-service.xml
11./WEB-INF/dispatcher-servlet.xml
12.</param-value>
13.</context-param>
14.<listener>
15.<listener-class>
16.org.springframework.web.context.ContextLoaderListener
17.</listener-class>
18.</listener>
19.<servlet>
20.<servlet-name>dispatcher</servlet-name>
21.<servlet-class>
22.org.springframework.web.servlet.DispatcherServlet
23.</servlet-class>
24.<load-on-startup>1</load-on-startup>
25.</servlet>   
26.<servlet-mapping>
27.<servlet-name>dispatcher</servlet-name>
28.<url-pattern>/</url-pattern>
29.</servlet-mapping>
30.</web-app>
通过以上5步,就配置好了Spring HTTP Invoker服务,地址:http://${serviceName}:${port}/${contextPath}/test



下面就是客户端怎么调用远程Spring HTTP Invoker服务。
1.创建服务接口及网络间传输的POJO类,为了方便,可以将服务器端创建好的的UserService.java和UserInfo.java拷贝到客户端,或打个jar包放到lib下。


2.配置HTTP Invoker的代理,用来调用远程服务,spring配置文件如下:
01.<bean id="httpService"
02.class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
03.<property name="serviceUrl">
04.<value>
05.http://${serviceName}:${port}/${contextPath}/test
06.</value>
07.</property>
08.<property name="serviceInterface" value="spring.invoker.service.UserService">
09.</property>
10.</bean>

3. 下面是使用Spring注解的方式,当然 ,你也可以使用配置的方式,把下面的类交给Spring管理,再把httpService通过set的方式,注入进来。
01.package spring.invoker;
02. 
03.import javax.annotation.Resource;
04. 
05.import org.springframework.stereotype.Controller;
06.import org.springframework.web.bind.annotation.RequestMapping;
07. 
08.import spring.invoker.service.UserService;
09. 
10.@Controller
11.public class InvokerAction {
12.@Resource private UserService httpService;
13. 
14.@RequestMapping(value="/httpTest")
15.public void testInvoker(){
16.System.out.println(httpService.getUserEmailbyName("xxx"));
17.}
18. 
19.}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多