Service 端:(注:org.springframework.web.servlet.DispatcherServlet)配置文件中.xml
<!-- RMI -->
<bean id="accountServiceImpl" class="com.spring.service.impl.AccountServiceImpl" />
<bean class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName" value="AccountService" />
<property name="service" ref="accountServiceImpl" />
<property name="serviceInterface" value="com.spring.service.AccountService" />
<property name="registryPort" value="1199" />
</bean>
接口类:
package com.spring.service;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.List;
import com.spring.model.Account;
public interface AccountService extends Remote {
public void insert(Account account) throws RemoteException;
public List<Account> getAccount(String name) throws RemoteException;
}
实现类:
package com.spring.service.impl;
import java.rmi.RemoteException;
import java.util.List;
import javax.annotation.Resource;
import com.spring.dao.AccountDao;
import com.spring.model.Account;
import com.spring.service.AccountService;
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
@Override
public void insert(Account account) throws RemoteException {
this.accountDao.insert(account);
}
@Override
public List<Account> getAccount(String name) throws RemoteException {
return null;
}
@Resource(name = "accountDaoImpl")
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
}
其他机器或项目客户端:需要在spring环境中
<bean id="accountService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://127.0.0.1:1199/AccountService" />
<property name="serviceInterface" value="com.spring.service.AccountService" />
</bean>
<bean id="simpleRMI" class="com.ccl.util.SimpleObject">
<property name="accountService" ref="accountService" />
</bean>
应用类:
package com.ccl.util;
import java.rmi.RemoteException;
import com.spring.model.Account;
import com.spring.service.AccountService;
public class SimpleObject {
private AccountService accountService;
public AccountService getAccountService() {
return accountService;
}
public void setAccountService(AccountService accountService) {
this.accountService = accountService;
}
public void insert() {
try {
Account a = new Account();
a.setName("client");
accountService.insert(a);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
调用:
ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
SimpleObject so = (SimpleObject) ac.getBean("simpleRMI");
so.insert();
从上可以看到:
要用到两个Service端的类,所以可以打成jar包加进项目就OK。