在Spring remoting中包含三種實現方式:http-invoker,hessian,burlap。具體的配置如下:
一、服務器端配置(applicationContext.xml):
<pre name="code"><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.plateno.service" />
<bean id="beanNameUrlHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<!-- 類似rmi方式,採用java序列化,數據量較大 -->
<bean name="/httpInvokerService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="remoteService"/>
<property name="serviceInterface" value="com.plateno.platform.service.RemoteService"/>
</bean>
<!-- 採用hessian序列化,數據量最小 -->
<bean name="/hessianService" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="remoteService"/>
<property name="serviceInterface" value="com.plateno.platform.service.RemoteService"/>
</bean>
<!-- xml傳輸數據,數據量比httpinvoker方式小 -->
<bean name="/burlapService" class="org.springframework.remoting.caucho.BurlapServiceExporter">
<property name="service" ref="remoteService"/>
<property name="serviceInterface" value="com.plateno.platform.service.RemoteService"/>
</bean>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.plateno.interceptor.CommonInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>
二、服務器端配置(web.xml)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
三、寫一個service,再寫一個serviceImpl就ok了。
四、客戶端配置:
<pre name="code"><bean id="httpInvokerProxyFactoryBean" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl" value="http://localhost:9999/server/httpInvokerService"/>
<property name="serviceInterface" value="com.plateno.platform.service.RemoteService"/>
</bean>
<bean id="hessianProxyFactoryBean" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl" value="http://localhost:9999/server/hessianService"/>
<property name="serviceInterface" value="com.plateno.platform.service.RemoteService"/>
</bean>
<bean id="burlapProxyFactoryBean" class="org.springframework.remoting.caucho.BurlapProxyFactoryBean">
<property name="serviceUrl" value="http://localhost:9999/server/burlapService"/>
<property name="serviceInterface" value="com.plateno.platform.service.RemoteService"/>
</bean>
五、測試類:
@Autowired
private RemoteService remoteService;
@RequestMapping(value="/test")
public ModelAndView test(@RequestParam(value="name", required=false) String name) {
UserDTO dto = remoteService.getUser("hello");
System.out.println(dto);
Map<String, Object> model = new HashMap<String, Object>();
return new ModelAndView("test/hello", model);
}