当前位置:首页 > 开发 > 系统架构 > 架构 > 正文

hessian系列之三:与Spring集成

发表于: 2014-02-17   作者:czj4451   来源:转载   浏览次数:
摘要: Spring封装了hessian客户端和服务端的通用代码,把实现者和调用者作为bean放到spring容器中管理,简化了开发。 实际生产情况,接口项目里面只定义接口,分别为服务端项目和客户端项目所引用: 1. 接口项目: pom.xml文件: <groupId>com.john.spring</groupId> <artifactId&
Spring封装了hessian客户端和服务端的通用代码,把实现者和调用者作为bean放到spring容器中管理,简化了开发。

实际生产情况,接口项目里面只定义接口,分别为服务端项目和客户端项目所引用:

1. 接口项目:

pom.xml文件:
<groupId>com.john.spring</groupId>
<artifactId>hessian-intf</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>


public interface AddService {
	public int add(int a, int b);
	
	public long add(long a, long b);
}


2. 服务端项目:

pom.xml文件:
  <groupId>com.john.spring</groupId>
  <artifactId>hessian-server</artifactId>
  <version>1.0.0</version>
  <packaging>war</packaging>

  <properties>
    <testng.version>6.4</testng.version>
    <hessian.version>4.0.7</hessian.version>
    <spring.version>3.1.2.RELEASE</spring.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>com.john.spring</groupId>
      <artifactId>hessian-intf</artifactId>
      <version>1.0.0</version>
    </dependency>
    <dependency><!-- DispatcherServlet在spring-webmvc包中 -->
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>com.caucho</groupId>
      <artifactId>hessian</artifactId>
      <version>${hessian.version}</version>
    </dependency>
  </dependencies>


public class AddServiceImpl implements AddService {  
	@Override
	public int add(int a, int b) {
		System.out.println("add(int a, int b) is invoked");
		return a + b;
	}

	@Override
	public long add(long a, long b) {
		System.out.println("add(long a, long b) is invoked");
		return a + b;
	}  
}


web.xml
<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"
	version="2.5">
  <servlet>
  	<servlet-name>hessian</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/hessian-simple.xml</param-value>
	</init-param>
	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>hessian</servlet-name>
	<url-pattern>*.htm</url-pattern>
  </servlet-mapping>
</web-app>


hessian-simple.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 
  <bean id="addService.htm" class="com.john.spring.hessian.service.impl.AddServiceImpl" />
 
  <bean name="/addService.htm" class="org.springframework.remoting.caucho.HessianServiceExporter">
    <property name="service" ref="addService.htm" />
    <property name="serviceInterface" value="com.john.spring.hessian.service.intf.AddService" />
  </bean>
</beans>


启动服务端项目:
浏览器输入:http://localhost/hessian/addService.htm,查看是否可以调用。

3. 客户端项目:

pom.xml文件
  <groupId>com.john.spring</groupId>
  <artifactId>hessian-client</artifactId>
  <version>1.0.0</version>
  <packaging>jar</packaging>

  <properties>
    <testng.version>6.4</testng.version>
    <hessian.version>4.0.7</hessian.version>
    <spring.version>3.1.2.RELEASE</spring.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>com.john.spring</groupId>
      <artifactId>hessian-intf</artifactId>
      <version>1.0.0</version>
    </dependency>
    <dependency><!-- HessianProxyFactoryBean在spring-web包中 -->
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>com.caucho</groupId>
      <artifactId>hessian</artifactId>
      <version>${hessian.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${spring.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>${testng.version}</version>
      <scope>test</scope>
    </dependency>
  </dependencies>


spring-beans.xml文件
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 
  <bean id="addService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
    <property name="serviceUrl" value="http://localhost:8080/hessian/addService.htm" />
    <property name="serviceInterface" value="com.john.spring.hessian.service.intf.AddService" />
	<!-- By default the value is false. As a matter of fact, it doesn't influence the test result, the overloaded method still be supported -->
	<property name="overloadEnabled" value="false" />
  </bean>
</beans>


测试类:
@ContextConfiguration(locations={"classpath:conf/spring/spring-beans.xml"})
public class AddServiceTest {
	
	@Autowired
	AddService addService;

	@Test
	public void test() {
		int r = addService.add(4, 5);
		System.out.println("The result is " + r);
	}
}


运行测试方法,查看是否调用成功。

附:
客户端Hessian bean的配置也可以放到接口项目中,这样一来,调用hessian的工程只需添加对接口项目的引用,而后在spring配置文件中导入:
<import resource=”classpath*:conf/spring/spring-*.xml” />

hessian系列之三:与Spring集成

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
Hessian入门(与Spring集成) By:wtang 说明 : 1.<span style="font-size: 7pt; line-height: nor
Hessian入门(与Spring集成) 说明 : 1.<span style="font-size: 7pt; line-height: normal; font
BizTalk学习笔记系列之三:企业集成应用和BizTalk ——.BizTalk学习笔记系列之三 Aaron.Gao,2006&l
接上文Spring IBatis Struts2 集成之二, 本篇写Spring与Struts2的集成。集成Spring Struts2 的整个
Spring框架对控制反转(Inversion of Control,IoC)进行了实现。 Spring IOC容器管理的对象被称作bea
Spring事务这个东西比之前的AOP和MVC都要复杂的多,而且Spring声明式的事务是非常常用的一个功能. 一
hi,boys and girls,i come back again! 承接上篇: Spring3MVC+MyBatis+ExtJs3整合开发系列之二:菜
通过实现 org.aopalliance.intercept.MethodInterceptor接口来实现环绕通知: public class CarAroun
通过实现 org.aopalliance.intercept.MethodInterceptor接口来实现环绕通知: public class CarAroun
通过实现 org.aopalliance.intercept.MethodInterceptor接口来实现环绕通知: public class CarAroun
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号