Spring RMI暴露服务


Service 端:(注:org.springframework.web.servlet.DispatcherServlet)配置文件中.xml

  1. <!-- RMI -->  
  2. <bean id="accountServiceImpl" class="com.spring.service.impl.AccountServiceImpl" />  
  3.   
  4. <bean class="org.springframework.remoting.rmi.RmiServiceExporter">  
  5.     <property name="serviceName" value="AccountService" />  
  6.     <property name="service" ref="accountServiceImpl" />  
  7.     <property name="serviceInterface" value="com.spring.service.AccountService" />  
  8.     <property name="registryPort" value="1199" />  
  9. </bean>  
接口类:
  1. package com.spring.service;  
  2.   
  3. import java.rmi.Remote;  
  4. import java.rmi.RemoteException;  
  5. import java.util.List;  
  6.   
  7. import com.spring.model.Account;  
  8.   
  9. public interface AccountService extends Remote {  
  10.   
  11.     public void insert(Account account) throws RemoteException;  
  12.   
  13.     public List<Account> getAccount(String name) throws RemoteException;  
  14.   
  15. }  
实现类:
  1. package com.spring.service.impl;  
  2.   
  3. import java.rmi.RemoteException;  
  4. import java.util.List;  
  5.   
  6. import javax.annotation.Resource;  
  7.   
  8. import com.spring.dao.AccountDao;  
  9. import com.spring.model.Account;  
  10. import com.spring.service.AccountService;  
  11.   
  12. public class AccountServiceImpl implements AccountService {  
  13.   
  14.     private AccountDao accountDao;  
  15.   
  16.     @Override  
  17.     public void insert(Account account) throws RemoteException {  
  18.   
  19.         this.accountDao.insert(account);  
  20.     }  
  21.   
  22.     @Override  
  23.     public List<Account> getAccount(String name) throws RemoteException {  
  24.         return null;  
  25.     }  
  26.   
  27.     @Resource(name = "accountDaoImpl")  
  28.     public void setAccountDao(AccountDao accountDao) {  
  29.         this.accountDao = accountDao;  
  30.     }  
  31.   
  32. }  

其他机器或项目客户端:需要在spring环境中

  1. <bean id="accountService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">  
  2.     <property name="serviceUrl" value="rmi://127.0.0.1:1199/AccountService" />  
  3.     <property name="serviceInterface" value="com.spring.service.AccountService" />  
  4. </bean>  
  5.   
  6. <bean id="simpleRMI" class="com.ccl.util.SimpleObject">  
  7.     <property name="accountService" ref="accountService" />  
  8. </bean>  

应用类:

  1. package com.ccl.util;  
  2.   
  3. import java.rmi.RemoteException;  
  4.   
  5. import com.spring.model.Account;  
  6. import com.spring.service.AccountService;  
  7.   
  8. public class SimpleObject {  
  9.   
  10.     private AccountService accountService;  
  11.   
  12.     public AccountService getAccountService() {  
  13.         return accountService;  
  14.     }  
  15.   
  16.     public void setAccountService(AccountService accountService) {  
  17.         this.accountService = accountService;  
  18.     }  
  19.   
  20.     public void insert() {  
  21.   
  22.         try {  
  23.             Account a = new Account();  
  24.             a.setName("client");  
  25.             accountService.insert(a);  
  26.         } catch (RemoteException e) {  
  27.             e.printStackTrace();  
  28.         }  
  29.   
  30.     }  
  31.   
  32. }  

调用:

  1. ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");  
  2.   
  3. SimpleObject so = (SimpleObject) ac.getBean("simpleRMI");  
  4.   
  5. so.insert();  
从上可以看到:

要用到两个Service端的类,所以可以打成jar包加进项目就OK。

相关内容