使用spring和rmi開發遠程服務的類的應用(此處可以一般的應用程式或web應用程式)
步驟:
1。建立服務端服務的介面的和實作類別的應用
2。在spring的設定檔中必須配置rmi的資訊
3。發布和部署rmi和spring整合的服務資訊。
4。用戶端訪問的服務
開始開發遠程服務資訊:
服務介面:
package cn.com.huawei.rmi.service;
import java.util.List;
/**
*k開發服務介面類
* @author bailonggang
* 2009-3-16
* 下午11:34:20
*/
public interface IUserService {
public List getUsernames();
}
package cn.com.huawei.rmi.service.impl;
import java.util.ArrayList;
import java.util.List;
import cn.com.huawei.rmi.service.IUserService;
/**
* 開發服務介面實作類別的
* @author bailonggang
* 2009-3-16
* 下午11:35:45
*/
public class UserService implements IUserService{
public List getUsernames()
{
List<String> usernames=new ArrayList<String>();
usernames.add("xiaobai");
usernames.add("xiaoli");
return usernames;
}
}
服務端的spring的配置如下:
<?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-2.0.xsd">
<!-- 遠程服務實作類別的介面 -->
<bean id="userservicetarget"
class="cn.com.huawei.rmi.service.impl.UserService" />
<!--
遠程服務類的對象的
-->
<bean id="Userservice-rmi"
class="org.springframework.remoting.rmi.RmiServiceExporter">
<!-- 遠程服務的名稱 -->
<property name="serviceName">
<value>UserService</value>
</property>
<!-- 遠程服務的連接埠 -->
<property name="registryPort">
<value>6335</value>
</property>
<!--
遠程服務的類
-->
<property name="service">
<ref bean="userservicetarget" />
</property>
<!--
遠程服務介面
-->
<property name="serviceInterface">
<value>cn.com.huawei.rmi.service.IUserService</value>
</property>
</bean>
</beans>
用戶端的配置資訊
用戶端的服務配置的屬性檔案
client.properties
# Properties file with server URL settings for remote access.
# Applied by PropertyPlaceholderConfigurer from "clientContext.xml".
serverName=localhost
httpPort=8080
rmiPort=1199
serverPath=SpringRMI
serviceName=UserService
用戶端服務的配置spring檔案的應用
<?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-2.0.xsd">
<!--
遠程服務的資訊配置的屬性檔案
-->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>client.properties</value>
</property>
</bean>
<bean id="rmiclient" class="cn.com.huawei.rmi.client.RMIClient">
<property name="userservice">
<ref local="proxyuserservice" />
</property>
</bean>
<!--
遠程服務的代理類的
-->
<bean id="proxyuserservice"
class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<!-- 遠程服務的url -->
<property name="serviceUrl">
<value>
rmi://${serverName}:${rmiPort}/${serviceName}
</value>
</property>
<!-- 遠程服務的介面類的 -->
<property name="serviceInterface">
<value>cn.com.huawei.rmi.service.IUserService</value>
</property>
</bean>
</beans>
用戶端服務調用遠程服務類
package cn.com.huawei.rmi.client;
import java.util.List;
import cn.com.huawei.rmi.service.IUserService;
/**
* 用戶端服務類的
* @author bailonggang
* 2009-3-16
* 下午11:43:27
*/
public class RMIClient {
private IUserService userservice;
public IUserService getUserservice() {
return userservice;
}
public void setUserservice(IUserService userservice) {
this.userservice = userservice;
}
public List getUsernames() {
return this.userservice.getUsernames();
}
}
部署發布,即可: