Spring版本2.5.6.SEC01
1.服务端:
需要如下jar包:spring.jar spring-webmvc.jar
IPersonService.java
- publicinterfaceIPersonService{
- publicStringqueryPersonName();
- }
PersonServiceImpl.java
- publicclassPersonServiceImplimplementsIPersonService{
- publicStringqueryPersonName(){
- return"test";
- }
- }
service-config.xml
- <?xmlversion="1.0"encoding="UTF-8"?>
- <!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN2.0//EN""http://www.springframework.org/dtd/spring-beans-2.0.dtd">
- <beans>
- <beanid="personService"
- class="com.travelsky.angel.service.impl.PersonServiceImpl"/>
- <beanid="serviceExporter"
- class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
- <propertyname="service"ref="personService"/>
- <propertyname="serviceInterface"
- value="com.travelsky.angel.service.IPersonService"/>
- </bean>
- <beanid="urlMapping"
- class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
- <propertyname="mappings">
- <props>
- <propkey="/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
- publicclassHttpInvokerClient{
- publicstaticvoidmain(String[]args){
- ApplicationContextcontext=newClassPathXmlApplicationContext("httpinvoker-client.xml");
- IPersonServiceservice=(IPersonService)context.getBean("personServiceProxy");
- Stringresult=service.queryPerson();
- System.out.println(result);
- }
- }
httpinvoker-client.xml
- <?xmlversion="1.0"encoding="UTF-8"?>
- <!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN2.0//EN""http://www.springframework.org/dtd/spring-beans-2.0.dtd">
- <beans>
- <beanid="personServiceProxy"
- class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
- <propertyname="serviceUrl"
- value="http://127.0.0.1:8080/ProjectContextRoot/person.service"/>
- <propertyname="serviceInterface"
- value="com.travelsky.angel.service.IPersonService"/>
- </bean>
- </beans>
注意:ProjectContextRoot对应.mymetadata中的context-root,如果修改,需要重新启动Eclipse
转自:http://up2pu.iteye.com/blog/988871