spring 对hessian支持

来源:互联网 发布:淘宝q币自动充值软件 编辑:程序博客网 时间:2024/05/22 14:40

 近来在看spring,还真不错对一些高级应用封装的很透明,不用深入了解就能实现功能,把自己写的一个demo放在这里,等于做个笔记。

1>.接口类

package com.olive.test.rms;

public interface HelloService {
    public String helloWorld();

    public String sayFromSomeBody(String someBodyName);
    void sayHello();
}

2>.实现类

package com.olive.test.rms;

public interface HelloServiceImp {
    public String helloWorld(){

         return "From HelloServiceImp";

    }

    public String sayFromSomeBody(String somebody){

         return  somebody+" say from HelloServiceImp";

   }
   public  void sayHello(){

         System.out.println("Hello I am HelloServiceImp");

   }


}

 

3>.以上写出了接口和实现类,接下来就应该在web应用中暴露服务,因为hessian是对http协议的远程调用。所以一般此服务是部署到web应用中,配置/WEB-INF/web.xml,通过配置一个servlet来暴露服务,真的很简单呀。

 

<servlet>
  <servlet-name>helloService</servlet-name>
  <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
  <init-param>
     <param-name>service-class</param-name>
     <param-value>com.olive.rms.test.HelloServiceImp</param-value>
  </init-param>
</servlet>
<servlet-mapping>
  <servlet-name>helloService</servlet-name>
  <url-pattern>/helloService</url-pattern>
</servlet-mapping>

简单的一个servlet就部署了hessian服务,只要启动此web应用,在客户端就可以自由调用了。

 

4>.客户端测试代码

 

package com.olive.test.rms;

 

import com.caucho.hessian.client.HessianProxyFactory;
import com.caucho.hessian.server.HessianServlet;
public class Test {

public static void main(String args []){
   try   String url = "http://localhost:8080/LightJ2ee/helloService";
   HessianProxyFactory factory = new HessianProxyFactory();
   HelloService hs=(HelloService)factory.create(com.olive.test.rms.HelloService.class, url);
   System.out.println(hs);
   hs.sayHello();//服务器控制台会输出
   System.out.println(hs.sayFromSomeBody("olive"));
   System.out.println("Call Ok.");
   }catch(Exception e){
    e.printStackTrace();
   }
  }
}

输出结果:

 

HessianProxy[http://localhost:8080/LightJ2ee/helloService]

olive say from HelloServiceImp

Call Ok

以上只是使用HessianServlet暴露hessian服务,还有可以通过spring的方式来实现。具体如下。

 

1>. 如上

2>.如上

3>.在你的web应用下面建一个spring 配置文件(其实是spring web 中DispatcherServlet对应配置文件),起命为WEB-INF/hello-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"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="helloService" class="com.olive.rms.HelloServiceImp"/>

<bean name="/HelloService" class="org.springframework.remoting.caucho.HessianServiceExporter">
    <property name="service" ref="helloService"/>
    <property name="serviceInterface" value="com.olive.rms.test.HelloService"/>
</bean>
</beans>

4>.配置你web应用中的WEB-INF/ web.xml,配置spring web映射文件

<servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/HelloService</url-pattern>
</servlet-mapping>

 

以上红色部分有对应关系,必须一样,以上服务端部署完毕,客户端测试代码如上4>

 

以上是使用HessianServiceExporter和spring web DispatcherServlet暴露hessian服务,还有一种是通过根据你web应用的上下文来创建一个HessianServiceExporter 。具体如下。

 

1>.同上

2>.同上

3>.创建应用上下文应用配置文件(spring 配置文件)/WEB-INF/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"
 xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="helloService" class="com.olive.rms.HelloServiceImp"/>

<bean name="helloExporter" class="org.springframework.remoting.caucho.HessianServiceExporter">
    <property name="service" ref="helloService"/>
    <property name="serviceInterface" value="com.olive.rms.test.HelloService"/>
</bean>
</beans>

4>.配置web应用中的/WEB-INF/web.xml文件

<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>WEB-INF/applicationContext.xml</param-value>
 </context-param>
 <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
 
 <servlet>
    <servlet-name>helloExporter</servlet-name>
    <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>helloExporter</servlet-name>
    <url-pattern>/HelloService</url-pattern>
</servlet-mapping>

以上服务器端配置完毕,测试代码同上4>.

 

个人感觉应该是第一种方法更简单和平常的servlet没多大区别,第二种方法必须把spring web配置导入进了,有些时候可能不需要,同样的第三种方便必须把spring web应用上下文导入进了。

 

日志中就可以看的出来

第二种

log4j:WARN No appenders could be found for logger (org.springframework.web.servlet.DispatcherServlet).
log4j:WARN Please initialize the log4j system properly.
2009-8-31 23:12:01 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring FrameworkServlet 'hello'

 

第三种

信息: Reloading context [/LightJ2ee]
log4j:WARN No appenders could be found for logger (org.springframework.core.CollectionFactory).
log4j:WARN Please initialize the log4j system properly.
2009-8-31 23:30:23 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring root WebApplicationContext