English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Intégration Spring et RMI

Spring RMI vous permet de rendre les services公开 via l'infrastructure RMI.

Spring utilise org.springframework.remoting.rmi. RmiProxyFactoryBean et org.springframework.remoting.rmi. RmiServiceExporter Classe.

RmiServiceExporter

Il fournit des services d'exportation pour les objets RMI. Vous pouvez accéder à ce service via RMI classique ou RmiProxyFactoryBean.

RmiProxyFactoryBean

C'est un bean de fabrique de proxy RMI. Il expose un service d'agent que l'on peut utiliser comme une référence de bean.

Exemple d'intégration Spring et RMI

Voyons les étapes simples pour intégrer une application Spring avec RMI:

Calculation.java CalculationImpl.java applicationContext.xml client-beans.xml Host.java Client.java


Jar fichiers nécessaires

Pour exécuter cet exemple, vous devez charger:

jar fichier Spring Core jar fichier Spring Remoting jar fichier Spring AOP

Téléchargez tous les fichiers jar de Spring, y compris core, web, aop, mvc, j2ee, remoting, oxm, jdbc, orm, etc.


1、Calculation.java

C'est une interface simple qui contient une méthode multidimensionnelle.

package com.w3codebox;
public interface Calculation {
int cube(int number);
}

2、CalculationImpl.java

Cette classe fournit l'implémentation de l'interface Calculation.

package com.w3codebox;
public class CalculationImpl implements Calculation{
    @Override
    public int cube(int number) {
        return number*number*number;
    }
}

3、applicationContext.xml

Dans ce fichier xml, nous définissons les classes CalculationImpl et RmiServiceExporter La classe définit le bean. Nous devons fournir des valeurs pour les propriétés suivantes de la classe RmiServiceExporter.

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>

4、client-beans.xml

Dans ce fichier xml, nous avons pour RmiProxyFactoryBean Définit le bean. Vous devez définir les deux propriétés de cette classe.

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>

5、Host.java

C'est juste pour obtenir l'instance d'ApplicationContext. Mais vous devez d'abord exécuter cette classe pour exécuter l'exemple.

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("En attente de requêtes");
}
}

6、Client.java

Cette classe obtient l'instance de Calculation et appelle cette méthode.

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));
}
}