Spring 提供简便的远程调用服务
一、下载hessian.jar包
二、服务器端配置
1、新建applicationContext-hessianServer.xml
<?xml version="1.0" encoding="UTF-8"?>
略....
<description>Hessian Configer</description>
<!--辅助类 可以不添加 -->
<bean id="lazySerializer" class="com.cmmb.mall.util.hessian.LazyHibernateSerializerFactory"></bean>
<!--merchantServiceExporter 暴露服务名 -->
<bean name="merchantServiceExporter" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="merchantService" />
<property name="serviceInterface" value="com.ws.api.MerchantService" />
<property name="serializerFactory" ref="lazySerializer"></property>
</bean>
<bean id="merchantService" class="com.ws.impl.MerchantServiceImpl" />
2、web.xml配置
<servlet>
<!--merchantServiceExporter 暴露服务名 相对应 -- >
<servlet-name>merchantServiceExporter</servlet-name>
<servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>merchantServiceExporter</servlet-name>
<url-pattern>/remoting/MerchantService</url-pattern>
</servlet-mapping>
三、客户端配置
1、新建applicationContext-hessianClient.xml
<?xml version="1.0" encoding="UTF-8"?>
略........
<bean id="merchantService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl" value="http://192.168.1.117:8080/merchantmanage/remoting/MerchantService" />
<property name="serviceInterface" value="com.cmmb.mall.ws.api.MerchantService" />
</bean>
四、测试
pubilc class Test{
//merchantService 配置的bean
ThirdProductService thirdProductService = (ThirdProductService) ac.getBean(" merchantService ");
public static void main(String[] args) {
//调用远程接口
List<ThirdProduct> tps = thirdProductService.listByStatus("ONLINE");
}
}
------------------------------------------------------------------------------
辅助类:
public class LazyHibernateSerializerFactory extends SerializerFactory {
public static String versionString = "3";
@SuppressWarnings("unchecked")
@Override
public Serializer getSerializer(Class cls) throws HessianProtocolException {
try {
if (versionString.startsWith("3")
&& Class.forName(
"org.hibernate.collection.PersistentCollection")
.isAssignableFrom(cls)) {
return new LazySerializerForHibernate3(cls);
} else if (versionString.startsWith("4")
&& Class
.forName(
"org.hibernate.collection.spi.PersistentCollection")
.isAssignableFrom(cls)) {
return new LazySerializerForHibernate4(cls);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.getSerializer(cls);
}
public static void main(String[] args) {
org.hibernate.engine.VersionValue hVersion ;
System.out.println(org.hibernate.engine.VersionValue.NEGATIVE);
}
}
http://www.eshow365.cn