Spring Httpinvoker简单Demo

来源:互联网 发布:mac 必装软件 编辑:程序博客网 时间:2024/06/05 02:52

一、服务端

需要的jar包:

spring.jar、spring-webmvc.jar、commons-logging-1.1.1.jar

新建java web工程  包结构如图1-1-2

Person类

public class Person implements Serializable{private String name;private String age;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}}

FirstDemoService类

public interface FirstDemoService {public String getString(String msg);public Person getInfo();}

FirstDemoServerImpl类

public class FirstDemoServerImpl implements FirstDemoService {public String getString(String msg) { String str = "正在请求调用...远程服务调用成功! " + msg;     return str;    }public Person getInfo(){Person stu=new Person();stu.setAge("23");stu.setName("Tom");return stu;}}

remote-servlet.xml 在WEB-INF下创建名称为remote-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><!-- 通过Spring HttpInvoker机制暴露远程访问服务 --><bean id="rmiService" class="com.leonard.httpinvoker.FirstDemoServerImpl" /><bean name="/remoteService"class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"><property name="service" ref="rmiService" /><property name="serviceInterface" value="com.leonard.httpinvoker.FirstDemoService" /></bean></beans>

在web.xml里配置spring httpinvoker

    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>/WEB-INF/FirstDemo-server.xml</param-value>    </context-param>    <!-- 配置DispatcherServlet -->    <servlet>        <servlet-name>remote</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <!-- 配置该Servlet随应用启动时候启动 -->        <load-on-startup>1</load-on-startup>    </servlet>    <!-- 配置DispatcherServlet映射的url -->    <servlet-mapping>        <servlet-name>remote</servlet-name>        <url-pattern>/remoting/*</url-pattern>    </servlet-mapping>



二、客户端

需要的jar包:

spring.jar、commons-logging-1.1.1.jar

Person类

新建java工程

public class Person implements Serializable{private String name;private String age;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAge() {return age;}public void setAge(String age) {this.age = age;}}

FirstDemoService类

public interface FirstDemoService {public String getString(String msg);public Person getInfo();}

TestFirstDemo客户端测试类

public class TestFirstDemo {   public static void main(String[] args) {   ApplicationContext applicationContext = new ClassPathXmlApplicationContext("firstdemo.xml");   FirstDemoService service = (FirstDemoService) applicationContext.getBean("remoteService");System.out.println("---------------返回字符串-----------------");System.out.println(service.getString("Httpinvoker,start.....!"));System.out.println("\n---------------返回自定义对象-----------------");Person stu=service.getInfo();System.out.println("姓名:"+stu.getName()+";年龄:"+stu.getAge());}}

在客户端工程中创建firstdemo.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 id="httpInvokerRequestExecutor" class="org.springframework.remoting.httpinvoker.SimpleHttpInvokerRequestExecutor" /><bean id="remoteService"class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"><property name="serviceUrl" value="http://localhost:8080/HttpInVokerServer/remoting/remoteService" /><property name="serviceInterface" value="com.leonard.httpinvoker.FirstDemoService" /><property name="httpInvokerRequestExecutor" ref="httpInvokerRequestExecutor" /></bean></beans>


先运行服务端然后运行客户端中TestFirstDemo类

结果:

 


                                                               图1-1-1


                                                  图1-1-2