Spring HttpInvoker(二)

一: 暴露服务(HttpRequestHandlerServlet 暴露服务)

package com.xx.service;

public interface ISayHelloService {

	/**
	 * @param name
	 * @return
	 */
	 String doSayHello(String name);
}
package com.xx.service.impl;

import com.xx.service.ISayHelloService;

public class DefaultSayHelloServiceImpl implements ISayHelloService {

	public String doSayHello(String name) {
		return "hello, " + name;
	}
}
web.xml
	<!-- ContextLoaderListener  -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/config/applicationContext-server.xml</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- HttpRequestHandlerServlet -->
	<!-- servlet-name 必须和暴露的seviceProxy bean id相同    -->
	<servlet>
		<servlet-name>sayHelloServiceExporter</servlet-name>
		<servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>sayHelloServiceExporter</servlet-name>
		<url-pattern>/sayHelloService.service</url-pattern>
	</servlet-mapping>
applicationContext-server.xml
	<bean id="defaultSayHelloService" class="com.xx.service.impl.DefaultSayHelloServiceImpl" />
	<!--  和web.xml servlet-name 一致 -->	
	<bean id="sayHelloServiceExporter" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter" >
		<property name="service" 			ref="defaultSayHelloService" />
		<property name="serviceInterface" 	value="com.xx.service.ISayHelloService" />
	</bean>
		

二:客户端

package com.xx.service;

public interface ISayHelloService {

	/**
	 * @param name
	 * @return
	 */
	 String doSayHello(String name);
}
applicationContext-client.xml
	<bean id="sayHelloService"  class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
		<property name="serviceUrl" 		value="http://localhost:8080/httpInvoker2/sayHelloService.service" />
		<property name="serviceInterface" 	value="com.xx.service.ISayHelloService"/>
	</bean>
public class ClientMain {
	
	public static void main(String[] args) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext-client.xml");
		ISayHelloService sayHelloService = applicationContext.getBean("sayHelloService", ISayHelloService.class);
		System.out.println(sayHelloService.doSayHello("王五"));
	}
}
posted on 2012-04-09 16:06  YangJin  阅读(167)  评论(0编辑  收藏  举报