发表于: 2020-07-21 22:11:15
1 1228
今天完成的事情:spring 整合 RMI
5.安 全:RMI使用Java内置的安全机制保证下载执行程序时用户系统的安全。
新建了两个项目 task8-Service 服务端WEB项目 和 客户端task8-Client Project项目
服务端:
目录结构:
接口 HelloRMIService
package rmi;
public interface HelloRMIService {
public String sayHi(String name);
}
接口实现类HelloRMIServiceImp
package rmi.imp;
import rmi.HelloRMIService;
public class HelloRMIServiceImp implements HelloRMIService {
@Override
public String sayHi(String name) {
return "Hi," + name;
}
}
applicationContext.xml
<bean id="helloRMIServiceImpl" class="rmi.imp.HelloRMIServiceImp"> </bean>
<!-- 将一个类发布为一个RMI服务 服务端使用RmiServiceExporter暴露RMI远程方法 -->
<bean id="RMIServiceTest" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="service" ref="helloRMIServiceImpl"></property>
<property name="serviceName" value="serverRmiTest"></property>
<property name="serviceInterface" value="rmi.HelloRMIService"></property>
<property name="registryPort" value="1021"></property>
</bean>
main 方法启动
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class RMIServiceTest {
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
context.getBean("RMIServiceTest");
}
}
控制台输出
可以看到一直在运行
可以看到Could not detect RMI registry - creating new one(找不到注册表,新建了一个注册表)
客户端:
目录结构
接口 HelloRMIService
package rmi;
public interface HelloRMIService {
public String sayHi(String name);
}
applicationContext.xml
!-- 客户端用RmiProxyFactoryBean间接调用远程方法。-->
<bean name="RMITest" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://127.0.0.1:1021/serverRmiTest"/>
<property name="serviceInterface" value="rmi.HelloRMIService"/>
</bean>
main 方法启动
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import rmi.HelloRMIService;
public class RMIServiceTest {
static Logger logger = Logger.getLogger(RMIServiceTest.class);
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
HelloRMIService helloRMIService = (HelloRMIService) context.getBean("RMITest");
logger.info(helloRMIService.sayHi("rmi"));
}
}
控制台输出
明天计划的事情:推进任务
评论