Spring远程访问
今天做了一个实验,用Spring实现对远程计算机上类和方法访问。一方面对Spring进行回顾,另一方面也是对JAVA远程访问的一个框架层次的了解!
Spring远程访问主要用接口技术来实现,所以要先定义一个服务接口。
package com.spring.rmi;
public interface RMIService {
public void show();
public String getString(String str);
}生成一个实现接口的类。
package com.spring.rmi.impl;
import com.spring.rmi.RMIService;
public class RMIServiceImpl implements RMIService{
public String getString(String str) {
return (str + "访问成功!");
}
public void show() {
System.out.println("这是服务器端的show()方法!");
}
}配置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-2.5.xsd">
<bean id="service" class="com.spring.rmi.impl.RMIServiceImpl">
</bean>
<bean id="exporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="service" ref="service"></property>
<property name="serviceName" value="service"></property>
<property name="serviceInterface" value="com.spring.rmi.RMIService"></property>
<property name="registryPort">
<value>2222</value>
</property>
</bean>
</beans>接下来就是配置客户端来获取服务器端的服务类。
<?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-2.5.xsd">
<bean id="serviceProxy" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://10.20.10.201:2222/service"></property>
<property name="serviceInterface" value="com.spring.rmi.RMIService">
</property>
</bean>
</beans>测试!!!
大家可以根据自己的需要修改IP和端口号,因为我是在同一台计算机上做实验所以测试方法就写到了一个方法中,大家可以根据需要自行修改测试方法!
package testRMI;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.rmi.RMIService;
import com.spring.rmi.impl.RMIServiceImpl;
public class testRMI {
public static void main(String args[]) {
ApplicationContext context = new ClassPathXmlApplicationContext("RMI-server.xml");
ApplicationContext context2 = new ClassPathXmlApplicationContext("rmi-client.xml");
RMIService server = (RMIService)context2.getBean("serviceProxy");
server.show();
System.out.println(server.getString("赵红伟"));
}
}大家如果需要源文件可以给我留言!
相关推荐
与卿画眉共浮生 2020-11-13
smalllove 2020-11-03
hellowordmonkey 2020-11-02
丽丽 2020-10-30
周太郎 2020-10-28
greensomnuss 2020-10-27
职业炮灰 2020-10-16
与卿画眉共浮生 2020-10-14
feinifi 2020-10-14
feinifi 2020-10-13
yangjinpingc 2020-10-09
davis 2020-09-29
RickyIT 2020-09-27
lisongchuang 2020-09-27
tangxiong0 2020-09-03
meleto 2020-08-17
幸运小侯子 2020-08-14
YangHuiLiang 2020-08-06