English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Spring RMI allows you to expose services through the RMI infrastructure.
Spring utilizes org.springframework.remoting.rmi. RmiProxyFactoryBean and org.springframework.remoting.rmi. RmiServiceExporter class.
RmiServiceExporter
It provides export services for RMI objects. This service can be accessed through ordinary RMI or RmiProxyFactoryBean.
RmiProxyFactoryBean
It is the factory bean for RMI proxy. It exposes an agent service that can be used as a bean reference.
Let's see the simple steps to integrate Spring application with RMI:
Calculation.java CalculationImpl.java applicationContext.xml client-beans.xml Host.java Client.java
To run this example, you need to load:
Spring Core jar file Spring Remoting jar file Spring AOP jar file
Download all the jar files of Spring, including core, web, aop, mvc, j2ee, remoting, oxm, jdbc, orm, etc.
1Calculation.java
This is a simple interface containing a method for multidimensional datasets.
package com.w3codebox; public interface Calculation { int cube(int number); }
2CalculationImpl.java
This class provides the implementation of the Calculation interface.
package com.w3codebox; public class CalculationImpl implements Calculation{ @Override public int cube(int number) { return number*number*number; } }
3applicationContext.xml
In this xml file, we define the CalculationImpl class and RmiServiceExporter This class defines a bean. We need to provide values for the following properties of the RmiServiceExporter class.
Service serviceInterface serviceName replaceExistingBinding registryPort
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="calculationBean" class="com.w3codebox.CalculationImpl"></bean> <bean class="org.springframework.remoting.rmi.RmiServiceExporter"> <property name="service" ref="calculationBean"></property> <property name="serviceInterface" value="com.w3codebox.Calculation"></property> <property name="serviceName" value="CalculationService"></property> <property name="replaceExistingBinding" value="true"></property> <property name="registryPort" value="1099></property> </bean> </beans>
4and client-beans.xml
In this xml file, we have RmiProxyFactoryBean Defined the bean. You need to define two properties of this class.
serviceUrl serviceInterface
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="calculationBean" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"> <property name="serviceUrl" value="rmi://localhost:1099/CalculationService"></property> <property name="serviceInterface" value="com.w3codebox.Calculation"></property> </bean> </beans>
5Host.java
It is just to get the instance of ApplicationContext. But you need to run this class first to run the example.
package com.w3codebox; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Host{ public static void main(String[] args){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); System.out.println("Waiting for requests"); } }
6、Client.java
This class gets an instance of Calculation and calls this method.
package com.w3codebox; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("client-beans.xml Calculation calculation = (Calculation)context.getBean("calculationBean"); System.out.println(calculation.cube(7)); } }