服务器端
1 编写interface和interface 实现类。在server端打包成jar文件分给客户端使用。
public interface IRemoteService {
String getMessage(String str);
}
public class WorkService implements IRemoteService {
@Override
public String getMessage(String str) {
// TODO Auto-generated method stub
return "from server side callback ... ..." + str;
}
}
2 在web.xml中配置spring的信息
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>exporter</servlet-name>
<servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>exporter</servlet-name>
<url-pattern>/remoting/exporter</url-pattern>
</servlet-mapping>
3 在applicationContext.xml中配置bean
<bean id="test" class="ccw.work.impl.WorkService"></bean>
<bean name="exporter" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="test"></property>
<property name="serviceInterface" value="ccw.work.IRemoteService"></property>
</bean>
web.xml中servlet的名称必须和applicationContext.xml中target exporter的名称一致。
HttpRequestHandlerServlet 通过getServletName()获取target bean的名称,将请求交给该bean进行处理(与spring mvc一样)。
客户端
1 applicationContext.xml中的配置
<bean id="remoteService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl" value="http://localhost:8080/springweb01/remoting/exporter" />
<property name="serviceInterface" value="ccw.work.IRemoteService" />
</bean>
2 测试代码
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
IRemoteService service = (IRemoteService) applicationContext.getBean("remoteService");
String msg = service.getMessage("haha,client");
System.out.println(msg);