hessian系列之三:与Spring集成

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” />

猜你喜欢

转载自czj4451.iteye.com/blog/2002066