服务端
packageremote.rmi;
importjava.io.Serializable;
publicclassAccountimplementsSerializable{
/**
*
*/
privatestaticfinallongserialVersionUID= 1L;
privateStringname;
publicString getName() {
returnname;
}
publicvoidsetName(String name) {
this.name= name;
}
}
远程接口:
packageremote.rmi;
importjava.rmi.Remote;
importjava.rmi.RemoteException;
importjava.util.List;
publicinterfaceAccountServiceextendsRemote{
publicvoidinsertAccount(Account account)throwsRemoteException;
publicList<Account> getAccount(String name)throwsRemoteException;
}
接口实现类:
packageremote.rmi;
importjava.rmi.RemoteException;
importjava.util.List;
publicclassAccountServiceImpimplementsAccountService{
@Override
publicList<Account> getAccount(String name)throwsRemoteException {
//TODOAuto-generated method stub
String text="Hello "+name;
System.out.println(text);
returnnull;
}
@Override
publicvoidinsertAccount(Account account)throwsRemoteException {
//TODOAuto-generated method stub
}
}
XML:
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEbeansPUBLIC"-//SPRING//DTD BEAN 2.0//EN""http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<beanid="accountService"class="remote.rmi.AccountServiceImp">
</bean>
<beanclass="org.springframework.remoting.rmi.RmiServiceExporter">
<propertyname="serviceName"value="AccountService"></property>
<propertyname="service">
<reflocal="accountService"/>
</property>
<propertyname="serviceInterface"
value="remote.rmi.AccountService">
</property>
<propertyname="registryPort"value="8080"></property>
</bean>
</beans>
服务器:
packageremote.rmi;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
publicclassService {
publicstaticvoidmain(String args[]) {
ApplicationContext context =newClassPathXmlApplicationContext(
"/remote/rmi/rmi.xml");
AccountServiceservice=(AccountService) context.getBean("accountService");
}
}
启动Service