---
title: "spring rmi配置"
author: "hao0610"
date: "2011-06-07T13:29:30Z"
tags: ["Bean", "Java", "Spring", "Tomcat", "XML"]
categories: ["企业架构"]
slug: "spring-rmi"
draft: false
lastmod: "2011-06-07T18:45:19Z"
id: 1071541
url: "https://hao0610.iteye.com/blog/1071541"
---

\[size=large]Spring Rmi配置\[/size] \[color=darkred]服务端:\[/color] 新建一个web项目, 并添加Spring能力. applicationContext.xml: \[code="xml"] \[/code] IBaseService: \[code="java"] package com.my.service; import com.my.po.User; public interface IBaseService { /\** * 简单Hello Word * @param name * @return \*/ public String getHelloWord(String name); /\** * 获得User对象 * @param user * @return \*/ public String getUser(User user); } \[/code] BaseServiceImpl: \[code="java"] package com.my.service.impl; import com.my.po.User; import com.my.service.IBaseService; public class BaseServiceImpl implements IBaseService { public String getHelloWord(String name) { return "欢迎"+name+"的到来!!!"; } public String getUser(User user) { return "名字:"+user.getName()+"---&gt;"+"年龄:"+user.getAge(); } } \[/code] User: 必须实现Serializable接口. \[code="java"] package com.my.po; import java.io.Serializable; public class User implements Serializable { private static final long serialVersionUID = 1L; private String name; private int age; public User(){} public User(String name, int age){ this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } \[/code] main: \[code="java"] package test; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.my.service.IBaseService; public class BaseServiceTest { /\** * @param args \*/ public static void main(String\[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); IBaseService ibs = (IBaseService) context.getBean("baseRmiService"); System.out.println("baseRmiService启动..."); } } \[/code] 把service接口和java bean打包为jar, 并添加到客户端里面. 启动main方法. 或者部署到tomcat里并启动. \[color=darkred]客户端:\[/color] 新建一个java项目或web项目, 并添加spring能力. applicationContext.xml: \[code="xml"] \[/code] Test: \[code="java"] package com.my.service; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.my.po.User; public class Test { public static void main(String\[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); IBaseService ibs = (IBaseService) context.getBean("baseService"); System.out.println(ibs.getHelloWord("hao")); User user = new User(); user.setName("chenghao"); user.setAge(21); System.out.println(ibs.getUser(user)); } } \[/code] 现在运行Test类就会在控制台打印出: 欢迎hao的到来!!! 名字:chenghao---&gt;年龄:21