Spring中HttpInvoker实例

本文介绍如何使用Spring 2.5.6版本搭建HTTP Invoker远程服务,包括服务端配置与客户端调用流程。服务端通过定义接口和服务实现类,利用Spring的HttpInvokerServiceExporter进行暴露;客户端则通过HttpInvokerProxyFactoryBean获取服务代理进行调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring版本2.5.6.SEC01

1.服务端:

需要如下jar包:spring.jar spring-webmvc.jar

IPersonService.java

public interface IPersonService { public String queryPersonName(); }

PersonServiceImpl.java

public class PersonServiceImpl implements IPersonService { public String queryPersonName() { return "test"; } }

service-config.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <bean id="personService" class="com.travelsky.angel.service.impl.PersonServiceImpl" /> <bean id="serviceExporter" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"> <property name="service" ref="personService" /> <property name="serviceInterface" value="com.travelsky.angel.service.IPersonService" /> </bean> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/person.service">serviceExporter</prop> </props> </property> </bean> </beans>

web.xml添加如下代码

<servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/service-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>*.service</url-pattern> </servlet-mapping>

2.客户端:

客户端需要应用服务端的bean和接口,另外需要spring.jar

HttpInvokerClient.java

public class HttpInvokerClient { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("httpinvoker-client.xml"); IPersonService service = (IPersonService) context.getBean("personServiceProxy"); String result = service.queryPerson(); System.out.println(result); } }

httpinvoker-client.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <bean id="personServiceProxy" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"> <property name="serviceUrl" value="http://127.0.0.1:8080/ProjectContextRoot/person.service" /> <property name="serviceInterface" value="com.travelsky.angel.service.IPersonService" /> </bean> </beans>

注意:ProjectContextRoot对应.mymetadata中的context-root,如果修改,需要重新启动Eclipse

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值