转自:http://up2pu.iteye.com/blog/988871
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