本文共 3104 字,大约阅读时间需要 10 分钟。
服务器端:
1.创建一个javabean对象,需要实现Serializable接口。(如果只是传输字符串可以不用创建javabean对象)
1 | import java.io.Serializable; |
2 | public class User implements Serializable { |
3 | private static final long serialVersionUID = 1L; |
4 | private String name; |
5 | private int age; |
6 | …… |
7 | } |
2.创建一个接口
01 | import com.my.po.User; |
02 | public interface IBaseService { |
03 | /** |
04 | * 简单Hello Word |
05 | * @param name |
06 | * @return |
07 | */ |
08 | public String getHelloWord(String name); |
09 | /** |
10 | * 获得User对象 |
11 | * @param user |
12 | * @return |
13 | */ |
14 | public String getUser(User user); |
15 | } |
3.实现类
01 | import com.my.po.User; |
02 | import com.my.service.IBaseService; |
03 | public class BaseServiceImpl implements IBaseService { |
04 | public String getHelloWord(String name) { |
05 | return "欢迎" +name+ "的到来!!!" ; |
06 | } |
07 | public String getUser(User user) { |
08 | return "名字:" +user.getName()+ "--->" + "年龄:" +user.getAge(); |
09 | } |
10 | } |
4.创建一个spring配置文件 applicationContext.xml
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | <? xml version = "1.0" encoding = "UTF-8" ?> |
03 | < beans xmlns = "" |
04 | xmlns:xsi = "" |
05 | xmlns:p = "" |
06 | xsi:schemaLocation=" |
07 | "> |
08 | < bean id = "baseRmiService" class = "com.my.service.impl.BaseServiceImpl" /> |
09 | < bean id = "baseServiceExporter" class = "org.springframework.remoting.rmi.RmiServiceExporter" > |
10 | <!-- 调用Service --> |
11 | < property name = "service" ref = "baseRmiService" /> |
12 | <!-- value值是提供给客户端调用 --> |
13 | < property name = "serviceName" value = "baseService" /> |
14 | <!-- service接口 --> |
15 | < property name = "serviceInterface" value = "com.my.service.IBaseService" /> |
16 | <!-- 注册端口 --> |
17 | < property name = "registryPort" value = "1200" /> |
18 | </ bean > |
19 | </ beans > |
5.main方法
1 | import org.springframework.context.support.ClassPathXmlApplicationContext; |
2 | import com.my.service.IBaseService; |
3 | public class BaseServiceTest { |
4 | public static void main(String[] args) { |
5 | ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml" ); |
6 | IBaseService ibs = (IBaseService) context.getBean( "baseRmiService" ); |
7 | System.out.println( "baseRmiService启动..." ); |
8 | } |
9 | } |
将User.java和IBaseService.java打成jar包,放到客户端里面。
客户端:
1.创建一个spring配置文件 applicationContext.xml
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | < beans xmlns = "" |
03 | xmlns:xsi = "" |
04 | xmlns:p = "" |
05 | xsi:schemaLocation=" |
06 | "> |
07 | < bean id = "baseService" class = "org.springframework.remoting.rmi.RmiProxyFactoryBean" > |
08 | <!-- baseService是调用服务端serviceName的value,1200是服务端注册的端口 --> |
09 | < property name = "serviceUrl" value = "" /> |
10 | <!-- service接口 --> |
11 | < property name = "serviceInterface" value = "com.my.service.IBaseService" /> |
12 | </ bean > |
13 | </ beans > |
2.main方法
01 | import org.springframework.context.support.ClassPathXmlApplicationContext; |
02 | import com.my.po.User; |
03 | public class ClientTest { |
04 | public static void main(String[] args) { |
05 | ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml" ); |
06 | IBaseService ibs = (IBaseService) context.getBean( "baseService" ); |
07 | System.out.println(ibs.getHelloWord( "hao" )); |
08 | User user = new User(); |
09 | user.setName( "chenghao" ); |
10 | user.setAge( 24 ); |
11 | System.out.println(ibs.getUser(user)); |
12 | } |
13 | } |
现在运行ClientTest类就会在控制台打印出:
欢迎hao的到来!!! 名字:chenghao--->年龄:24
转载地址:http://yatbb.baihongyu.com/