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
- Spring Httpinvoker简单Demo
- Spring HttpInvoker
- spring httpinvoker
- spring httpInvoker
- Spring中HttpInvoker实例
- Spring HttpInvoker 开发
- Spring HttpInvoker & EOFException
- Spring HttpInvoker(一)
- Spring HttpInvoker(二)
- Spring中HttpInvoker实例
- Spring HttpInvoker源码分析
- HttpInvoker in Spring
- spring httpInvoker 性能优化
- Spring Security简单Demo
- spring security简单demo .
- spring mvc简单demo
- spring简单的demo
- Spring AOP 简单demo
- HttpURLConnection
- error while loading shared libraries的解決方法
- 时域与频域
- 当电脑出现的验证码老是提示输入错误怎么办?
- Java与C/C++有什么区别
- Spring Httpinvoker简单Demo
- [Android] EditText 的 inputType属性(键盘类型)
- Java 实现HTML 页面转成image 图片
- xcode改工程名
- 淘宝技术发展8(分布式时代:服务化)
- 浅谈多进程多线程的选择
- 作为开发者,你不应该害怕的 8 件事
- 显示hibernate生成语句的参数
- hdu 1560