Rmi在Spring中的使用之RmiServiceExporter
2014-10-28 00:00
351 查看
今天面试被问及到一个只是简单听说过,但是没有去用过的东西,用了简短的一会时间去看了下Spring的rmi文档,大致实现方式有其下几种
1.org.springframework.remoting.rmi.RmiProxyFactoryBean
其使用的是rmi协议实现
实现过程,首先是服务端
定义一个导出类
rmi.xml
启动服务
接下来客户端如下
rmi.xml
启动程序
这样就可以在服务器端得到了RMI Test!
当我们在启动服务端的时候会发现,其控制台一直在运行状态,当结束后,还会有rmi进程在运行。其接口协议为rmi://hostname:1199/xxxxxxx
1.org.springframework.remoting.rmi.RmiProxyFactoryBean
其使用的是rmi协议实现
实现过程,首先是服务端
定义一个导出类
public interface AccountService { String getUsername(); }
public class AccountServiceImpl implements AccountService{ @Override public String getUsername() { return "RMI Test!"; } }
rmi.xml
<?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.xsd"> <bean id="accountService" class="example.AccountServiceImpl"> <!-- any additional properties, maybe a DAO? --> </bean> <bean class="org.springframework.remoting.rmi.RmiServiceExporter"> <!-- 这里的服务名可以随意填写但是必须和rmi://hostname:1199/xxxxxxx的xxxxxxx相同 --> <property name="serviceName" value="AccountService"/> <!-- 导出实体 --> <property name="service" ref="accountService"/> <!-- 导出接口,这个为导出接口,注意,客户端包名可以和这里不同,但是为了统一建议做一个coreInterface 包,以便以后维护方便 --> <property name="serviceInterface" value="example.AccountService"/> <!-- 端口号,默认为1099,这里注意占用问题 --> <property name="registryPort" value="1199"/> </bean> </beans>
启动服务
public class Main { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("rmi.xml"); AccountService service = context.getBean("accountService", AccountService.class); String userName = service.getUsername(); System.out.println(userName); } }
接下来客户端如下
public interface AccountService { String  7fe0 ;getUsername(); }
rmi.xml
<?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.xsd"> <bean id="accountService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"> <!-- 接收的rmi协议 --> <property name="serviceUrl" value="rmi://localhost:1199/AccountService"/> <!-- 接收的rmi协议的接口 --> <property name="serviceInterface" value="example.AccountService"/> </bean> </beans>
启动程序
public class Main { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("rmi.xml"); AccountService service = context.getBean("accountService", AccountService.class); String userName = service.getUsername(); System.out.println(userName); } }
这样就可以在服务器端得到了RMI Test!
当我们在启动服务端的时候会发现,其控制台一直在运行状态,当结束后,还会有rmi进程在运行。其接口协议为rmi://hostname:1199/xxxxxxx
相关文章推荐
- 使用Spring的RmiServiceExporter发布RMI服务设置registryHost报Connection refused to host问题解决
- RMI在Spring中的使用之HttpInvokerServiceExporter
- SpringRMI解析3-RmiServiceExporter逻辑细节
- 使用spring的MBeanExporter暴露hibernate的StatisticsService对象方法
- Spring RmiServiceExporter Can't connect to 127.0.0.2 issue
- Spring注解的使用和区别:@Component、@Service、@Repository、@Controller
- 第十三篇 在使用spring AOP中, Service中使用service出现的异常
- Spring RMI 源码浅析-RmiServiceExporter 导出服务
- spring3.0.5的rmi集成使用
- 如何使用spring将service层注入到servlet中去(how to use Spring to inject ur service layer into the servlet )
- Spring使用Quartz定时调度Job无法Autowired注入Service的解决方案
- 使用Spring的Service注入Dao接口需要注意的细节问题
- Spring注解的使用和区别:@Component、@Service、@Repository、@Controller
- Spring整合使用RMI-HelloWorld例子
- 使用junit4测试spring项目中service方法
- 使用Spring的Service注入Dao接口需要注意的细节问题
- Spring注解@Componen、@Repository@Service、@Controller区别与Spring2.5中使用注解装配属性and组件自
- Spring RMI 源码浅析-RmiServiceExporter 导出服务
- 使用Spring进行远程访问(rmi)与Web服务
- spring3.0.5的rmi集成使用