服务端:
RmiServer.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- RMI远程调用 -->
<bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName" value="MyRMIService" />
<property name="service" ref="rmiService" />
<property name="serviceInterface" value="com.test.ddy.service.IRMIService" />
<property name="registryPort" value="8088" />
</bean>
<bean id="rmiService" class="com.test.ddy.service.RMIServiceImpl" /></beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- RMI远程调用 -->
<bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName" value="MyRMIService" />
<property name="service" ref="rmiService" />
<property name="serviceInterface" value="com.test.ddy.service.IRMIService" />
<property name="registryPort" value="8088" />
</bean>
<bean id="rmiService" class="com.test.ddy.service.RMIServiceImpl" /></beans>
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<description>Spring公共配置</description>
<!-- 自动扫描 -->
<context:component-scan base-package="com.test.ddy" />
<mvc:annotation-driven />
<context:annotation-config />
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/pages/" p:suffix=".jsp" />
<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<import resource="RmiServer.xml" />
<import resource="spring-mybatis.xml" />
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<description>Spring公共配置</description>
<!-- 自动扫描 -->
<context:component-scan base-package="com.test.ddy" />
<mvc:annotation-driven />
<context:annotation-config />
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/pages/" p:suffix=".jsp" />
<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<import resource="RmiServer.xml" />
<import resource="spring-mybatis.xml" />
</beans>
定义服务接口:
public interface IRMIService {
String getUserName(String name);
String getUserName(String name);
}
定义实现类:
package com.test.ddy.service;
import org.apache.log4j.Logger;
public class RMIServiceImpl implements IRMIService {
private static final Logger logger = Logger.getLogger(RMIServiceImpl.class);
@Override
public String getUserName(String name) {
// TODO Auto-generated method stub
return "hello,"+name;
}
import org.apache.log4j.Logger;
public class RMIServiceImpl implements IRMIService {
private static final Logger logger = Logger.getLogger(RMIServiceImpl.class);
@Override
public String getUserName(String name) {
// TODO Auto-generated method stub
return "hello,"+name;
}
}
客户端:
RmiServer.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- RMI远程调用 -->
<bean id="iRMIService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://127.0.0.1:8088/MyRMIService" />
<property name="serviceInterface" value="com.test.ddy.service.IRMIService" />
</bean>
</beans>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- RMI远程调用 -->
<bean id="iRMIService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://127.0.0.1:8088/MyRMIService" />
<property name="serviceInterface" value="com.test.ddy.service.IRMIService" />
</bean>
</beans>
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<description>Spring公共配置</description>
<!-- 自动扫描 -->
<context:component-scan base-package="com.test.ddy" />
<mvc:annotation-driven />
<context:annotation-config />
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/pages/" p:suffix=".jsp" />
<import resource="spring-mybatis.xml"/>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<description>Spring公共配置</description>
<!-- 自动扫描 -->
<context:component-scan base-package="com.test.ddy" />
<mvc:annotation-driven />
<context:annotation-config />
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/pages/" p:suffix=".jsp" />
<import resource="spring-mybatis.xml"/>
</beans>
客户端的spring-mvc.xml中不需要引入RmiServer.xml
定义接口:
package com.test.ddy.service;
public interface IRMIService {
String getUserName(String name);
public interface IRMIService {
String getUserName(String name);
}
客户端不需要实现这个接口,
客户端Controller:
package com.test.ddy.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.test.ddy.model.User;
import com.test.ddy.service.IRMIService;
import com.test.ddy.service.IUserService;
/**
*/
@Controller
@RequestMapping("/index")
public class IndexController {
@RequestMapping(value = "/getRMI", method = RequestMethod.GET)
public String getRMI(HttpServletRequest request) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"classpath:RmiServer.xml");
IRMIService hs = (IRMIService) ctx.getBean("iRMIService");
String temp = hs.getUserName("my vincent ");
System.out.println(temp);
return "index";
}
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.test.ddy.model.User;
import com.test.ddy.service.IRMIService;
import com.test.ddy.service.IUserService;
/**
*/
@Controller
@RequestMapping("/index")
public class IndexController {
@RequestMapping(value = "/getRMI", method = RequestMethod.GET)
public String getRMI(HttpServletRequest request) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"classpath:RmiServer.xml");
IRMIService hs = (IRMIService) ctx.getBean("iRMIService");
String temp = hs.getUserName("my vincent ");
System.out.println(temp);
return "index";
}
}
客户端控制台输出结果:hi my vincent
如果要传递一个User对象,则需要进行序列化User implements Serializable
服务端:github https://github.com/vincentduan/SpringRMIServer.git
客户端:https://github.com/vincentduan/SpringRMIClient.git