Spring 和 RMI 集成

spring rmi整合:

spring可以将任意POJO对象设置为RMI对象,供外部程序访问。通过spring的AOP动态代理可以添加RMI的访问规则。spring的RMI导出器仅仅只是暴露了POJO对外部的访问,但其安全管理,权限控制等还需要自己处理,
但spring还是提供了访问点,可以通过访问点,应用自己的安全策略。 具体配置如下:

服务端:
接口类:
package com.rmi;

public interface UserService {

public abstract void insertUser(String username);

public abstract void delUser(String username);

}

具体实现类:
package com.rmi;

public class UserServiceImpl implements UserService {
/* (non-Javadoc)
* @see com.rmi.UserService#insertUser(java.lang.String)
*/
public void insertUser(String username) {
System.out.println("UserService.insertUser() inser user sucess..."
+ username);
}

/* (non-Javadoc)
* @see com.rmi.UserService#delUser(java.lang.String)
*/
public void delUser(String username) {
System.out.println("UserService.delUser() del username success......"
+ username);
}
}


applicatonContext-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" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="userservice" class="com.rmi.UserServiceImpl" /> <!-- 具体需要导出的RMI对象,其对象是一个POJO对象 -->
<bean id="userExporter" class="org.springframework.remoting.rmi.RmiServiceExporter"> <!-- RmiServiceExporter 采用spring的AOP动态代理技术将对rmi远程对象的调用转化成对目标对象的调用 -->
<property name="serviceName" value="UserService"></property> <!-- 注册的服务名 -->
<property name="service" ref="userservice"/><!-- 目标实现对象 -->
<property name="serviceInterface" value="com.rmi.UserService"></property><!-- 目标对象的接口类 -->
<property name="registryPort" value="1599" /><!-- 注册的端口,可以通过该端口获取到目标对象 -->
<property name="servicePort" value="458" /><!-- 用于进行通信的端口 -->
</bean>
</beans>
通过以上配置,就可以通过 rmi://localhost:1599/UserService访问到该rmi服务。

客户端配置:
1.客户端可以通过普通RMI访问策略访问有spring管理的服务器端。
2.客户端也可以通过spring的RmiProxyFactoryBean访问。
下面介绍第二种:

applicatonContext-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" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

<bean id="RmiUserService" class="org.springframework.remoting.rmi.RmiProxyFactoryBea">
<property name="serviceUrl" value="rmi://localhost:1599/UserService" />
<proeprty name="serviceInterface" value="com.rmi.UseService" />
</bean>
</beans>

通过以上配置就可以直接从springIOC容器中拿到远程RMI对象,此RmiUserService对象就可以看出是服务器端的UserServiceImpl对象。

通信逻辑:

RmiServiceExport 通过Spring的AOP将POJO暴露成RMI对象。客户端通过RmiProxyFactoryBean访问Rmi对象。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值