关于Spring--hessian远程调用接口服务配置

maven依赖hessian的jar包如下:

<dependency>
     <groupId>com.caucho</groupId>
     <artifactId>hessian</artifactId>
     <version>4.0.37</version>
</dependency>

服务端:

1、创建hession-servlet.xml文件

<?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:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
	
	<context:annotation-config />
	<!--   定义普通的bean实例,接口的实现类 -->
	<bean id="personImpl" class="com.test.service.impl.PersonServiceImpl"/>	
	<!--   使用HessianServiceExporter 将普通bean导出成Hessian服务,将接口暴露给外部给客户端调用,/personService.remote为远程调用接口的url -->
	<bean name="/personService.remote" class="org.springframework.remoting.caucho.HessianServiceExporter">
		<!--  需要导出的目标bean(导出接口的实现类) -->
		<property name="service"  ref="personImpl"/>
		<!--  Hessian服务的接口 -->
		<property name="serviceInterface" value="com.test.service.PersonService"/>
	</bean>
</beans>

2、配置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"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_test" version="3.0">
	<display-name>test</display-name>
	<context-param>
    	<param-name>webAppRootKey</param-name>
    	<param-value>test.root</param-value>
 	</context-param>
	
	<!-- springMVC配置 -->
	<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:config/springMVC-servlet.xml</param-value>
		</init-param>
		<load-on-startup>2</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springMVC</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
	<!-- hessian远程调用配置,设置mvc拦截器,用来拦截远程调用的接口的url -->
	<servlet>
		<servlet-name>hessian</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
                        <!--上面配置的xml文件-->
			<param-value>classpath:config/hessian-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>hessian</servlet-name>
                <!--拦截的路径-->
		<url-pattern>/hessian/*</url-pattern>
	</servlet-mapping>
  	
</web-app>

3、将暴露的接口(接口需要用到的实体类model)导出为jar包,供客户端直接引用

总结:

  • 通过springMVC的DispatherServlet控制Hessian服务接口的URL访问路径,即web.xml配置
  • 新建Hessian服务 例如:Service 和实现类 ServiceImpl
  • 配置Hessian服务的暴露,通过HessianServiceExporter
     

客户端:

1、引用服务端提供的jar包

2、applicationContext.xml文件的配置

<?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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 
		http://www.springframework.org/schema/mvc
		http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

	<!-- 启用注解 -->
	<context:annotation-config />
	
	<!-- 创建bean代理对象,调用远程接口服务,id:要与@Autowired注入的名称相同 -->
	<bean id="personService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
		<property name="overloadEnabled" value="true" />
		<!-- 调用的远程接口url -->
		<property name="serviceUrl">
			<value>http://127.0.0.1:8080/test/hessian/personService.remote</value>
		</property>
		<!-- 远程调用服务接口(本地的接口),该接口要与服务端的接口完全一致 -->
		<property name="serviceInterface">
			<value>com.test.service.PersonService</value>
		</property>
	</bean>
	
</beans>

3、客户端在Cotroller层可直接@autowired注入接口服务(hessian代理对象)

package com.myproject.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.test.entity.Person;
import com.test.service.PersonService;

@Controller
@RequestMapping("/test")
public class TestController {
	
	private static final Logger log = LoggerFactory.getLogger(TestController.class);
	
	/**
	 * hession接口代理对象
	 */
	@Autowired
	private PersonService personService;
	
	@RequestMapping("/test")
	@ResponseBody
	public List<Person> personList(){
		List<Person> list = personService.list();
		System.out.println("远程调用接口输出");
		return list;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值