服务器端A项目:
package com.remote;
public interface IRemoteService {
// TODO;测试远程调用方法
public String getString(String str);
}
package com.remote.impl;
public class RemoteServiceImpl implements IRemoteService {
private IMyService myService;
public String getString(String msg) {
String str = "远程服务调用成功........ " + msg;
return str;
}
}
配置remote-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 通过Spring HttpInvoker机制暴露远程访问服务 -->
<bean id="remotingService" class="com.remote.impl.RemoteServiceImpl">
<property name="myService" ref="myService"></property>
</bean>
<bean name="/remoteService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="remotingService"/>
<property name="serviceInterface" value="com.remote.IRemoteService"/>
</bean>
</beans>
web.xml中加入
<!-- 载入remote-servlet.xml文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>remote-servlet.xml</param-value>
</context-param>
<!-- 配置DispatcherServlet -->
<servlet>
<servlet-name>remote</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置该Servlet随应用启动时候启动 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 配置DispatcherServlet映射的url -->
<servlet-mapping>
<servlet-name>remote</servlet-name>
<url-pattern>/remoting/*</url-pattern>
</servlet-mapping>
客户端B项目:
package com.remote;
//此接口可以和服务器端接口的名字不一样,但内部必须和服务器端接口中方法名一致
public interface IRemoteService {
/**
* 测试远程调用的方法;
*/
public String getString(String str);
}
配置remote.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 通过Spring HttpInvoker机制代理远程访问服务 -->
<bean id="remoteService"
class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl"
value="http://localhost:8080/A项目/remoting/remoteService" />
<property name="serviceInterface" value="com.remote.IRemoteService" />
</bean>
</beans>
import com.remote.IRemoteService;
import com.opensymphony.xwork2.ActionSupport;
public class TestAction extends ActionSupport {
private IRemoteService remoteService;
private String result;
public String testRemote() {
String msg ="向服务器发送信息";
result = remoteService.getString(msg);
System.out.println("testRemote测试结果-------->" + result);
return SUCCESS;
}
public void setRemoteService(IRemoteService remoteService) {
this.remoteService = remoteService;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
}
在web.xml中加入
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>remote-servlet.xml</param-value>
</context-param>
在spring配置文件中加入
<!-- 测试远程调用 start -->
<bean id="TestAction" class="remotetest.TestAction"
scope="prototype">
<property name="remoteService">
<ref bean="remoteService" />
</property>
</bean>