¡¡¡¡4.´´½¨·þÎñ²¢Æô¶¯·þÎñ
1 import java.rmi.Naming; 2 import java.rmi.registry.LocateRegistry; 4 public class SetService { 6 public static void main(String[] args) { 8 try { 10 StudentService studentService=new StudentServiceImpl(); 12 LocateRegistry.createRegistry(5008);//¶¨Òå¶Ë¿ÚºÅ 14 Naming.rebind("rmi://127.0.0.1:5008/StudentService", studentService); 16 System.out.println("·þÎñÒÑÆô¶¯"); 18 } catch (Exception e) { 20 e.printStackTrace(); 22 } 24 } 26 } |
¡¡¡¡5. ´´½¨Ò»¸ö¿Í»§³ÌÐò½øÐÐRMIµ÷Óá£
1 import java.rmi.Naming; 3 import java.util.List; 5 public class GetService { 9 public static void main(String[] args) { 11 try { 13 StudentService studentService=(StudentService) Naming.lookup("rmi://127.0.0.1:5008/StudentService"); 15 List<Student> list = studentService.getList(); 17 for (Student s : list) { 19 System.out.println("ÐÕÃû£º"+s.getName()+",ÄêÁ䣺"+s.getAge()); 21 } 23 } catch (Exception e) { 25 e.printStackTrace(); 27 } 29 } 33 } |
¡¡¡¡6.¿ØÖÆ̨ÏÔʾ½á¹û
¡¡¡¡=============¿ØÖÆ̨============
¡¡¡¡ÐÕÃû£ºÕÅÈý,ÄêÁ䣺15
¡¡¡¡ÐÕÃû£ºÀîËÄ,ÄêÁ䣺20
¡¡¡¡===============================
¡¡¡¡ÔÚSpringÖÐÅäÖÃRmi·þÎñ
¡¡¡¡½«RmiºÍSpring½áºÏÆðÀ´ÓõĻ°£¬±ÈÉÏÃæʵÏÖRmi·þÎñÒª·½±ãµÄ¶à¡£
¡¡¡¡1.Ê×ÏÈÎÒÃǶ¨Òå½Ó¿Ú£¬´Ëʱ¶¨ÒåµÄ½Ó¿Ú²»ÐèÒª¼Ì³ÐÆäËû½Ó¿Ú£¬Ö»ÊÇÒ»¸öÆÕͨµÄ½Ó¿Ú
¡¡¡¡1 package service;
¡¡¡¡3 import java.util.List;
¡¡¡¡5 public interface StudentService {
¡¡¡¡7 List<Student> getList();
¡¡¡¡9 }
¡¡¡¡2.¶¨ÒåÒ»¸öÀ࣬ʵÏÖÕâ¸ö½Ó¿Ú£¬Õâ¸öÀàÒ²Ö»ÐèʵÏÖ²½ÖèÒ»¶¨ÒåµÄ½Ó¿Ú£¬²»ÐèÒª¶îÍâµÄ²Ù×÷
1 package service; 4 import java.util.ArrayList; 6 import java.util.List; 9 public class StudentServiceImpl implements StudentService { 11 public List<Student> getList() { 13 List<Student> list=new ArrayList<Student>(); 15 Student s1=new Student(); 17 s1.setName("ÕÅÈý"); 19 s1.setAge(15); 21 Student s2=new Student(); 23 s2.setName("ÀîËÄ"); 25 s2.setAge(20); 27 list.add(s1); 29 list.add(s2); 31 return list; 33 } 35 } |