Spring 企業級開發應用———rmi和spring的整合的應用

來源:互聯網
上載者:User

使用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();
 }

}

 

部署發布,即可:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.