ITeye博客

Hessian入门(与Spring集成)

adrianSet · 2013-09-23 13:45 · java · java / spring3.0 · 企业架构 · Markdown

Hessian入门(与Spring集成)

                                                                                                                     

说明 :

1.    讲述如何配置Hessian的服务器端(与Spring集成).

2.    讲述客户端如何调用

①   使用HessianProxyFactory  Hessian代理工厂直接调用

②   使用HessianProxyFactoryBean Hessian代理工厂Bean来完成接口调用.

 

1.    讲述如何配置Hessian的服务器端(与Spring集成).

 

 

 

 

 

接口定义类: com.wtang.isay. Isay:

 

 

[java] view plaincopy

 

  1. package com.wtang.isay;
  2. public interface Isay {
  3. public String sayHello(String arg1,String arg2);
  4. }

 

 

接口具体实现类: com.wtang.isay. IsayImpl

 

[java] view plaincopy

 

  1. package com.wtang.isay;
  2. public class IsayImpl implements Isay {
  3. public String sayHello(String arg1, String arg2) {
  4. return "Hello:" + arg1 + arg2;
  5. }
  6. }

 

配置Web.xml:

 

[java] view plaincopy

 

  1. <servlet>
  2. <servlet-name>remote</servlet-name>
  3. <!-- 使用Spring的代理Servlet -->
  4. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  5. <init-param>
  6. <param-name>namespace</param-name>
  7. <param-value>classes/remote-servlet</param-value>
  8. </init-param>
  9. <load-on-startup>1</load-on-startup>
  10. </servlet>
  11. <servlet-mapping>
  12. <servlet-name>remote</servlet-name>
  13. <url-pattern>/remote/*</url-pattern>
  14. </servlet-mapping>

 

配置remote-servlet.xml[该文件位于src目录下,即编译后存在与classes下]:

 

[java] view plaincopy

 

  1. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
  2. <beans>
  3. <!-- 接口的具体实现类 -->
  4. <bean id="impl" class="com.wtang.isay.IsayImpl" />
  5. <!-- 使用Spring的HessianServie做代理 -->
  6. <bean name="/helloSpring"
  7. class="org.springframework.remoting.caucho.HessianServiceExporter">
  8. <!-- service引用具体的实现实体Bean-->
  9. <property name="service" ref="impl" />
  10. <property name="serviceInterface" value="com.wtang.isay.Isay" />
  11. </bean>
  12. <!-- 可以配置多个HessianServiceExporter代理Bean -->
  13. </beans>

 

注:

      这个文件为什么叫remote-servlet.xml呢?

因为我们在web.xml中有配置:<servlet-name>remote</servlet-name>。

所以remote-servlet.xml的文件名必须以

<servlet-name>中配置的servlet-name作为文件名的开头,

且文件名的格式必须是[servlet-name]-servlet.xml格式,否则检测不到。

    即:

<param-value>classes/remote-servlet</param-value>

所以文件名为remote-servlet.xml。

 

 

2.    讲述客户端如何调用

        ① 使用HessianProxyFactory  Hessian代理工厂直接调用

 即:

       

[java] view plaincopy

 

  1. package com.wtang.test;
  2. import java.net.MalformedURLException;
  3. import com.caucho.hessian.client.HessianProxyFactory;
  4. import com.wtang.isay.Isay;
  5. public class NormalClient {
  6. public static void main(String[] args) throws MalformedURLException {
  7. //Spring Hessian代理Servelet
  8. String url = "http://localhost:8080/HessianSpring/remote/helloSpring";
  9. HessianProxyFactory factory = new HessianProxyFactory();
  10. Isay api = (Isay) factory.create(Isay.class, url);
  11. System.out.println(api.sayHello("chen", "weitang"));
  12. }
  13. }

 

 

输出Hello:chenweitang

 

2.    讲述客户端如何调用

        ② 使用HessianProxyFactoryBean Hessian代理工厂Bean来完成接口调用.

配置客户端 remote-client.xml:

 

[java] view plaincopy

 

  1. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
  2. <beans>
  3. <!-- 客户端Hessian代理工厂Bean -->
  4. <bean id="clientSpring" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
  5. <!-- 请求代理Servlet路径 -->
  6. <property name="serviceUrl">
  7. <value>http://localhost:8080/HessianSpring/remote/helloSpring</value>
  8. </property>
  9. <!-- 接口定义 -->
  10. <property name="serviceInterface">
  11. <value>com.wtang.isay.Isay</value>
  12. </property>
  13. </bean>
  14. </beans>

 

 

调用:

 

[java] view plaincopy

 

  1. package com.wtang.test;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. import com.wtang.isay.Isay;
  5. public class SpringClient {
  6. public static void main(String[] args) {
  7. ApplicationContext contex = new ClassPathXmlApplicationContext(
  8. "remote-client.xml");
  9. // 获得客户端的Hessian代理工厂bean
  10. Isay i = (Isay) contex.getBean("clientSpring");
  11. System.out.println(i.sayHello("chen", "weitang"));
  12. }
  13. }

 

 

输出Hello:chenweitang

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。