Hessian入门(与Spring集成)
说明 :
- 讲述如何配置Hessian的服务器端(与Spring集成).
- 讲述客户端如何调用
① 使用HessianProxyFactory Hessian代理工厂直接调用
② 使用HessianProxyFactoryBean Hessian代理工厂Bean来完成接口调用.
- 讲述如何配置Hessian的服务器端(与Spring集成).
接口定义类: com.wtang.isay. Isay:
- package com.wtang.isay;
- public interface Isay {
- public String sayHello(String arg1,String arg2);
- }
接口具体实现类: com.wtang.isay. IsayImpl
- package com.wtang.isay;
- public class IsayImpl implements Isay {
- public String sayHello(String arg1, String arg2) {
- return "Hello:" + arg1 + arg2;
- }
- }
配置Web.xml:
- <servlet>
- <servlet-name>remote</servlet-name>
- <!-- 使用Spring的代理Servlet -->
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>namespace</param-name>
- <param-value>classes/remote-servlet</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>remote</servlet-name>
- <url-pattern>/remote/*</url-pattern>
- </servlet-mapping>
配置remote-servlet.xml[该文件位于src目录下,即编译后存在与classes下]:
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
- <!-- 接口的具体实现类 -->
- <bean id="impl" class="com.wtang.isay.IsayImpl" />
- <!-- 使用Spring的HessianServie做代理 -->
- <bean name="/helloSpring"
- class="org.springframework.remoting.caucho.HessianServiceExporter">
- <!-- service引用具体的实现实体Bean-->
- <property name="service" ref="impl" />
- <property name="serviceInterface" value="com.wtang.isay.Isay" />
- </bean>
- <!-- 可以配置多个HessianServiceExporter代理Bean -->
- </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。
- 讲述客户端如何调用
① 使用HessianProxyFactory Hessian代理工厂直接调用
即:
- package com.wtang.test;
- import java.net.MalformedURLException;
- import com.caucho.hessian.client.HessianProxyFactory;
- import com.wtang.isay.Isay;
- public class NormalClient {
- public static void main(String[] args) throws MalformedURLException {
- //Spring Hessian代理Servelet
- String url = "http://localhost:8080/HessianSpring/remote/helloSpring";
- HessianProxyFactory factory = new HessianProxyFactory();
- Isay api = (Isay) factory.create(Isay.class, url);
- System.out.println(api.sayHello("chen", "weitang"));
- }
- }
输出Hello:chenweitang
- 讲述客户端如何调用
② 使用HessianProxyFactoryBean Hessian代理工厂Bean来完成接口调用.
配置客户端 remote-client.xml:
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
- <!-- 客户端Hessian代理工厂Bean -->
- <bean id="clientSpring" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
- <!-- 请求代理Servlet路径 -->
- <property name="serviceUrl">
- <value>http://localhost:8080/HessianSpring/remote/helloSpring</value>
- </property>
- <!-- 接口定义 -->
- <property name="serviceInterface">
- <value>com.wtang.isay.Isay</value>
- </property>
- </bean>
- </beans>
调用:
- package com.wtang.test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.wtang.isay.Isay;
- public class SpringClient {
- public static void main(String[] args) {
- ApplicationContext contex = new ClassPathXmlApplicationContext(
- "remote-client.xml");
- // 获得客户端的Hessian代理工厂bean
- Isay i = (Isay) contex.getBean("clientSpring");
- System.out.println(i.sayHello("chen", "weitang"));
- }
- }
输出Hello:chenweitang
转自:http://blog.csdn.net/chenweitang123/article/details/6334097