Spring远程调用

服务器端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>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值