jar包依赖
<!--远程接口依赖-->
<!-- https://mvnrepository.com/artifact/com.caucho/hessian -->
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.51</version>
</dependency>
一
远程模拟:(没大用)
被连接的
<!--远程调用接口-->
<servlet>
<servlet-name>hessian</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<init-param>
<param-name>home-class</param-name>
<param-value>com.test.PersonServiceImpl</param-value>
</init-param>
<init-param>
<param-name>home-api</param-name>
<param-value>com.test.PersonServciceI</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>hessian</servlet-name>
<url-pattern>/hessian</url-pattern>
</servlet-mapping>
见一个测试类: 对象不对 自己调整。
连接测试:
HessianProxyFactory factory = new HessianProxyFactory();
CustomerService service = (CustomerService) factory.create(CustomerService.class,"http://localhost:8080/remoting/customer");
List<Customer> list = service.findnoassociationCustomers();
System.out.println(list);
远程项目:要有上边的jar包
一。建好pojo类 和接口 如上图:要和上一个项目包名类名一致
二。配置远程服务的代理对象:其实就是创建 customerService Bean ,将接口实现类拿过来。
(在SpringMVC中配置)
<bean id="customerService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<!-- 注入接口类型 -->
<property name="serviceInterface" value="com.gyf.crm.service.CustomerService"/>
<!-- 服务访问路径 -->
<property name="serviceUrl" value="http://localhost:8080/remoting/customer"/>
</bean>
被连接的项目:
在web.xml中配置
<servlet>
<servlet-name>remoting</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>remoting</servlet-name>
<url-pattern>/remoting/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
在remoting-servlet.xml 配置 (在web info 下)
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 业务类 -->
<bean id="customerService" class="com.gyf.crm.service.impl.CustomerServiceImpl" />
<!-- 注册hessian服务 -->
<bean id="/customer" class="org.springframework.remoting.caucho.HessianServiceExporter">
<!-- 业务接口实现类 -->
<property name="service" ref="customerService" />
<!-- 业务接口 -->
<property name="serviceInterface" value="com.gyf.crm.service.CustomerService" />
</bean>
</beans>