首页 > 代码库 > spring配置rmi

spring配置rmi

Spring Rmi配置
1、服务器端
新建一个Dynamic 、Web Project,导入支持Spring jar包。在WEB-INF下新建applicationContent.xml(必须加上<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">,否则会报错),代码如下:

Java代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">   
  3. <beans>   
  4.        
  5.     <bean id="baseRmiService" class="com.wym.service.impl.BaseServiceImpl">   
  6.     </bean>   
  7.     <bean id="baseServiceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">   
  8.         <!-- 调用Service -->   
  9.         <property name="service" ref="baseRmiService"></property>   
  10.         <!-- value值是给用户调用 -->   
  11.         <property name="serviceName" value=http://www.mamicode.com/"baseService"></property>   
  12.         <!-- service 接口 -->   
  13.         <property name="serviceInterface" value=http://www.mamicode.com/"com.wym.service.BaseService"></property>   
  14.         <!-- 注册端口号 -->   
  15.         <property name="registryPort" value=http://www.mamicode.com/"1200"></property>   
  16.     </bean>   
  17. </beans>  
<?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="baseRmiService" class="com.wym.service.impl.BaseServiceImpl">    </bean>    <bean id="baseServiceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">    	<!-- 调用Service -->    	<property name="service" ref="baseRmiService"></property>    	<!-- value值是给用户调用 -->    	<property name="serviceName" value="http://www.mamicode.com/baseService"></property>    	<!-- service 接口 -->    	<property name="serviceInterface" value="http://www.mamicode.com/com.wym.service.BaseService"></property>    	<!-- 注册端口号 -->    	<property name="registryPort" value="http://www.mamicode.com/1200"></property>    </bean></beans>



BaseService.java

Java代码 复制代码 收藏代码
  1. package com.wym.service;   
  2.   
  3. import com.wym.bi.User;   
  4.   
  5. public interface BaseService {   
  6.        
  7.     public String getHelloword(String name);   
  8.        
  9.     public String getUser(User user);   
  10. }  
package com.wym.service;import com.wym.bi.User;public interface BaseService {		public String getHelloword(String name);		public String getUser(User user);}



BaseServiceImpl.java

Java代码 复制代码 收藏代码
  1. package com.wym.service.impl;   
  2.   
  3. import com.wym.bi.User;   
  4. import com.wym.service.BaseService;   
  5.   
  6. public class BaseServiceImpl implements BaseService {   
  7.        
  8.     public String getHelloword(String name){   
  9.         return "Welcome to Shanghai," + name + "!";   
  10.     }   
  11.   
  12.     public String getUser(User user){   
  13.         return "name: " + user.getName() + "------>" + "age: " + user.getAge();   
  14.     }   
  15. }  
package com.wym.service.impl;import com.wym.bi.User;import com.wym.service.BaseService;public class BaseServiceImpl implements BaseService {		public String getHelloword(String name){		return "Welcome to Shanghai," + name + "!";	}	public String getUser(User user){		return "name: " + user.getName() + "------>" + "age: " + user.getAge();	}}



User:必须实现Serializable接口

Java代码 复制代码 收藏代码
  1. package com.wym.bi;   
  2.   
  3. import java.io.Serializable;   
  4.   
  5. public class User implements Serializable {     
  6.        
  7.     private static final long serialVersionUID = 1L;     
  8.      
  9.     private String name;     
  10.     private int age;     
  11.      
  12.     public User(){}     
  13.          
  14.     public User(String name, int age){     
  15.         this.name = name;     
  16.         this.age = age;     
  17.     }     
  18.          
  19.     public String getName() {     
  20.         return name;     
  21.     }     
  22.      
  23.     public void setName(String name) {     
  24.         this.name = name;     
  25.     }     
  26.      
  27.     public int getAge() {     
  28.         return age;     
  29.     }     
  30.      
  31.     public void setAge(int age) {     
  32.         this.age = age;     
  33.     }     
  34.   
  35. }  
package com.wym.bi;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;      }  }



main方法:

Java代码 复制代码 收藏代码
  1. package com.wym.service;   
  2.   
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  4. import com.wym.service.BaseService;   
  5.   
  6. public class BaseServiceTest {   
  7.   
  8.     /**  
  9.      * @param args  
  10.      */  
  11.     public static void main(String[] args) {   
  12.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");   
  13.         BaseService baseService = (BaseService) context.getBean("baseRmiService");   
  14.         System.out.println("baseRmiService start...");   
  15.     }   
  16.   
  17. }  
package com.wym.service;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.wym.service.BaseService;public class BaseServiceTest {	/**	 * @param args	 */	public static void main(String[] args) {		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");		BaseService baseService = (BaseService) context.getBean("baseRmiService");		System.out.println("baseRmiService start...");	}}


把service借口和java bean打成jar包放到客户端的里面。
启动main方法,或者将service发布到服务器。

2、客户端
新建一个java项目或者web项目,导入Spring jar包。新建applicationContext.xml,代码如下:

Java代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">   
  3. <beans>   
  4.   
  5.     <bean id="baseService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">   
  6.         <!-- baseService是调用服务端serviceName的value -->   
  7.         <property name="serviceUrl" value=http://www.mamicode.com/"rmi://192.168.0.37:1200/baseService"></property>   
  8.         <!-- service接口 -->   
  9.         <property name="serviceInterface" value=http://www.mamicode.com/"com.wym.service.BaseService"></property>   
  10.     </bean>   
  11. </beans>  
<?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="baseService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">		<!-- baseService是调用服务端serviceName的value -->		<property name="serviceUrl" value="rmi://192.168.0.37:1200/baseService"></property>		<!-- service接口 -->		<property name="serviceInterface" value="http://www.mamicode.com/com.wym.service.BaseService"></property>	</bean></beans>


192.168.0.37是服务器的IP地址。

main方法

Java代码 复制代码 收藏代码
  1. package wym.client;   
  2.   
  3. import com.wym.bi.User;   
  4. import com.wym.service.BaseService;   
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  6. public class ClientTest {   
  7.   
  8.     public static void main(String[] args) {   
  9.         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("file:d:/tnt_project/TEST/conf/applicationContext.xml");   
  10.         BaseService baseService = (BaseService) context.getBean("baseService");   
  11.         System.out.println(baseService.getHelloword("Yunmin Wu"));   
  12.         User user = new User();   
  13.         user.setName("Dan Qiao");   
  14.         user.setAge(48);   
  15.         System.out.println(baseService.getUser(user));   
  16.     }   
  17. }  
package wym.client;import com.wym.bi.User;import com.wym.service.BaseService;import org.springframework.context.support.ClassPathXmlApplicationContext;public class ClientTest {    public static void main(String[] args) {        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("file:d:/tnt_project/TEST/conf/applicationContext.xml");        BaseService baseService = (BaseService) context.getBean("baseService");        System.out.println(baseService.getHelloword("Yunmin Wu"));        User user = new User();        user.setName("Dan Qiao");        user.setAge(48);        System.out.println(baseService.getUser(user));    }}



关于Spring的applicationContent.xml文件的详解,请点此链接:http://ithead.iteye.com/admin/blogs/1461565。
运行main方法,在控制台就能看见结果了。

spring配置rmi