一、服务端
1、web.xml配置
加上如下代码:
<servlet>
<servlet-name>accountExporter</servlet-name>
<servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>accountExporter</servlet-name>
<url-pattern>/remoting/AccountService</url-pattern>
</servlet-mapping>
2、在spring配置文件加入如下代码
<bean id="dataServernew" class="com.imchooser.infoms.rmi.impl.DataServerImpl" scope="prototype">
<property name="baseDao" ref="baseDaoImpl" />
</bean>
<bean name="accountExporter" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="dataServernew"/>
<property name="serviceInterface" value="com.imchooser.infoms.rmi.DataServer"/>
</bean>
其中DataServer为暴露服务的接口bean;baseDaoImpl为具体实现类。
二、客户端
1、在spring配置文件加上如下代码
<bean id="httpInvokerProxy" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl" value="http://服务端ip:服务端端口/项目名称/remoting/AccountService"/>
<property name="serviceInterface" value="com.imchooser.infoms.rmi.DataServer"/>
</bean>
<bean id="rmiserverImpl"
class="com.imchooser.infoms.rmi.impl.RMIServerImpl" autowire="byName" >
<property name="data" ref="httpInvokerProxy"/>
</bean>
其中DataServer为客户端的接口bean。本地接口不需要创建这个接口的实现类。
客户端接口bean必须与服务端名称一样,然后在客户端的实现类RMIServerImpl中创建个DataServer的变量,变量名为data。
这样在客户端调用DataServer的方法,就会自动调用到服务端相对应的方法
转载于:https://blog.51cto.com/695956013/1238515