1. 程式人生 > >關於Spring--hessian遠端呼叫介面服務配置

關於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包,供客戶端直接引用

客戶端:

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;
	}
}