歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Spring RMI暴露服務

Spring RMI暴露服務

日期:2017/3/1 10:23:19   编辑:Linux編程

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

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

其他機器或項目客戶端:需要在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. <bean id="simpleRMI" class="com.ccl.util.SimpleObject">
  6. <property name="accountService" ref="accountService" />
  7. </bean>

應用類:

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

調用:

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

要用到兩個Service端的類,所以可以打成jar包加進項目就OK。

Copyright © Linux教程網 All Rights Reserved