hession入门及注入spring

  categories:资料  author:

hessian入门教程

一. hession入门

1 创建服务器端

导入hessian包

【第1步】 编写服务器端接口com.hessian.server.Hello.java

package com.hessian.server;

/**

* hessian 服务接口,用于客户端调用

* Hessian建立的远程调用,是对服务的接口进行调用

*

* @author莫小哆_ly 2012-1-16

*/

publicinterface Hello {

 

/**

* 测试字符串

* @return

*/

public String sayHello();

}

【第2步】 编写接口实现类com.hessian.server.impl.HelloImpl.java

package com.hessian.server.impl;

import com.hessian.server.Hello;

/**

* 实现服务端接口

*

* @author莫小哆_ly 2012-1-16

*/

publicclass HelloImpl implements Hello {

private String hello = “Hello World”;

 

@Override

public String sayHello() {

returnhello;

}

 

//Getter/Setter

public String getHello() {

returnhello;

}

publicvoid setHello(String hello) {

this.hello = hello;

}

}

【第3步】 修改配置文件WEB-INF/web.xml

<?xmlversion=”1.0″encoding=”UTF-8″?>

<web-appxmlns: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″>

<display-name>hessianServer</display-name>

<!– 用户键入的URL不包含action名称、JSP页面或其他资源,依次寻找文件 –>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

<servlet>

<servlet-name>hello</servlet-name>

<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>

<init-param>

<param-name>home-api</param-name>

<param-value>com.hessian.server.Hello</param-value>

</init-param>

<init-param>

<param-name>home-class</param-name>

<param-value>com.hessian.server.impl.HelloImpl</param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>hello</servlet-name>

<url-pattern>/hello</url-pattern>

</servlet-mapping>

</web-app>

【第4步】 编写服务端的测试jsp页面index.jsp

<%@pagelanguage=”java”contentType=”text/html; charset=UTF-8″

pageEncoding=”UTF-8″%>

<!– 引入类 –>

<%@pageimport=”java.util.*”%>

<%@pageimport=”com.caucho.hessian.client.HessianProxyFactory”%>

<%@pageimport=”com.hessian.server.Hello”%>

<!DOCTYPEhtmlPUBLIC”-//W3C//DTD HTML 4.01 Transitional//EN””http://www.w3.org/TR/html4/loose.dtd”>

<html>

<head>

<metahttp-equiv=”Content-Type”content=”text/html; charset=UTF-8″>

<title>hessian</title>

</head>

<body>

<%

// Hessian代理工厂

HessianProxyFactory factory = new HessianProxyFactory();

// 控制台打印

System.out.println(“test”);

// jsp页面打印

out.println(“<h3>test</h3>”);

//获得访问配置的Servlet路径

String url = (“http://” + request.getServerName() + “:” + request.getServerPort()

+ request.getContextPath() + “/hello”);

System.out.println(url);

out.println(“request.getServerName() ==== ” + request.getServerName() + “<br/>”);

out.println(“request.getServerPort() ==== ” + request.getServerPort() + “<br/>”);

out.println(“request.getContextPath() ==== ” + request.getContextPath() + “<br/>”);

//使用Hessian工厂获得接口的具体实现类

Hello hello = (Hello) factory.create(Hello.class, url);

out.println(“<h2>” + hello.sayHello() + “<h2>”);

%>

</body>

</html>

2 创建客户端

导入hessian包

【第1步】 编写客户端接口com.hessian.server.Hello.java

与服务器端保持一致

package comhessian.server;

/**

*

* hessian 服务接口,用于客户端调用

* Hessian建立的远程调用,是对服务的接口进行调用

*

* @author 莫小哆_ly 2012-1-16

*/

publicinterface Hello {

 

/**

* 测试字符串

* @return

*/

public String sayHello();

}

【第2步】 编写客户端测试类com.hessian.client.simpleTest.java

package com.hessian.client;

import java.net.MalformedURLException;

import com.caucho.hessian.client.HessianProxyFactory;

import com.hessian.server.Hello;

/**

* 客户端测试

*

* @author 莫小哆_ly 2012-1-16

*/

publicclass SimpleTest {

publicstaticvoid main(String[] args) {

String url = “http://10.21.140.47:8080/myHessianServer/hello”;

// Hessian代理工厂

HessianProxyFactory factory = new HessianProxyFactory();

Hello hello;

try {

// 使用Hessian工厂获得接口的具体实现类

hello = (Hello) factory.create(Hello.class, url);

// 直接调用接口方法,实际通过服务器端执行实现类方法,返回结果

System.out.println(“远程调用:” + hello.sayHello());

} catch (MalformedURLException e) {

e.printStackTrace();

}

}

}

3 执行

run as Server — myHessianServer

run as Java Aplication — myHessianClient

4 后记

二. spring 同hessian基础

1. Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能. 相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。传输对象时,需要序列化(implement Serialization)

2. 在进行基于Hessian的项目开发时,应当注意以下几点:

▲JAVA服务器端必须具备以下几点:

·包含Hessian的jar包

·设计一个接口,用来给客户端调用(服务器端和客户端均有)

·实现该接口的功能

·配置web.xml,配好相应的servlet

·对象必须实现Serializable 接口

·对于复杂对象可以使用Map的方法传递

▲客户端必须具备以下几点:

·java客户端包含Hessian.jar的包。

·具有和服务器端结构一样的接口。包括命名空间都最好一样

·利用HessianProxyFactory调用远程接口。

 

hessian+Spring远程过程调用 教程

1 创建服务器端

导入jar包

【第1步】 创建实体类suning.entity.User.java

package suning.entity;

/**

*

* 用户信息实体类

*

* @author 莫小哆_ly 2012-2-14

*/

publicclass User {

// 用户名

private String userName;

// 密码

private String passWord;

// 注册时间

private String registeredTime;

public User() {

}

// 用于登录

public User(String userName, String passWord) {

this.userName = userName;

this.passWord = passWord;

}

// 用于注册

public User(String userName, String passWord, String registeredTime) {

this.userName = userName;

this.passWord = passWord;

this.registeredTime = registeredTime;

}

// Getter/Setter

public String getUserName() {

returnuserName;

}

publicvoid setUserName(String userName) {

this.userName = userName;

}

public String getPassWord() {

returnpassWord;

}

publicvoid setPassWord(String passWord) {

this.passWord = passWord;

}

public String getRegisteredTime() {

returnregisteredTime;

}

publicvoid setRegisteredTime(String registeredTime) {

this.registeredTime = registeredTime;

}

}

【第2步】 创建服务接口suning.server.Login.java

package suning.server;

/**

*

* hessian 服务接口,用于客户端调用

* Hessian建立的远程调用,是对服务的接口进行调用

*

* @author 莫小哆_ly 2012-1-16

*/

publicinterface Login {

/**

* 登录服务

* @return

*/

public String login(String username,String password);

}

【第3步】 编写服务接口实现类suning.server.impl.LoginImpl.java

package suning.server.impl;

import suning.entity.User;

import suning.server.Login;

/**

* 实现服务端接口

*

* @author 莫小哆_ly 2012-1-16

*/

publicclass LoginImpl implements Login {

@Override

public String login(String username, String password) {

//可用数据库连接查询

User user = new User(“admin”, “admin”);

if ((user.getUserName().equals(username)) && (user.getPassWord().equals(password))) {

return”SUCCESS”;

}

return”ERROR”;

}

}

【第4步】 编写Spring配置文件WEB-INF/remoting-servlet.xml

<?xmlversion=”1.0″encoding=”UTF-8″?>

<beansxmlns=”http://www.springframework.org/schema/beans”

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”xmlns:context=”http://www.springframework.org/schema/context”

xmlns:tx=”http://www.springframework.org/schema/tx”xmlns:aop=”http://www.springframework.org/schema/aop”

xsi:schemaLocation=”http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-2.5.xsd”>

<!– 接口实现类路径 –>

<beanid=”loginImpl”class=”suning.server.impl.LoginImpl”/>

<!– 创建远程调用的服务/loginService,服务中注入LoginImpl的实现。 远程需要获得这个远程调用的服务(/loginService,注意名字前有个/ –>

<beanname=”/loginService”

class=”org.springframework.remoting.caucho.HessianServiceExporter”>

<!– 声明接口路径 –>

<propertyname=”serviceInterface”value=”suning.server.Login”/>

<!– 接口与实现类配置 –>

<propertyname=”service”ref=”loginImpl”/>

</bean>

</beans>

【第5步】 编写Web项目配置文件WEB-INF/web.xml

<?xmlversion=”1.0″encoding=”UTF-8″?>

<web-appxmlns: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″>

 

<display-name>myHessianSpringServer</display-name>

<!– 用户键入的URL不包含action名称、JSP页面或其他资源,依次寻找文件 –>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

 

<servlet>

<servlet-name>remoting</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>remoting</servlet-name>

<url-pattern>/remoting/*</url-pattern>

</servlet-mapping>

<servlet>

<servlet-name>login</servlet-name>

<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>

<init-param>

<param-name>home-api</param-name>

<param-value>suning.server.Login</param-value>

</init-param>

<init-param>

<param-name>home-class</param-name>

<param-value>suning.server.impl.LoginImpl</param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>login</servlet-name>

<url-pattern>/login</url-pattern>

</servlet-mapping>

</web-app>

【第6步】 编写服务端测试jsp页面index.jsp

<%@pagelanguage=”java”contentType=”text/html; charset=UTF-8″

pageEncoding=”UTF-8″%>

<!– 引入类 –>

<%@pageimport=”com.caucho.hessian.client.HessianProxyFactory”%>

<%@pageimport=”suning.server.Login”%>

<!DOCTYPEhtmlPUBLIC”-//W3C//DTD HTML 4.01 Transitional//EN””http://www.w3.org/TR/html4/loose.dtd”>

<html>

<head>

<metahttp-equiv=”Content-Type”content=”text/html; charset=UTF-8″>

<title>hessian-spring-server</title>

</head>

<body>

<%

// Hessian代理工厂

HessianProxyFactory factory = new HessianProxyFactory();

// 控制台打印

System.out.println(“test”);

// jsp页面打印

out.println(“<h3>test</h3>”);

//获得访问配置的Servlet路径

String url = (“http://” + request.getServerName() + “:” + request.getServerPort()

+ request.getContextPath() + “/remoting/loginService”);

System.out.println(url);

//使用Hessian工厂获得接口的具体实现类

Login login = (Login) factory.create(Login.class, url);

out.println(“<h2>” + login.login(“admin”,”admin”) + “<h2>”);

%>

</body>

</html>

2 创建客户端,不注入Spring

【Java Project】

导入hessian-4.0.7.jar

【第1步】 创建客户端接口suning.server.Login.java

package suning.server;

/**

*

* hessian 服务接口,用于客户端调用

* Hessian建立的远程调用,是对服务的接口进行调用

*

* @author 莫小哆_ly 2012-1-16

*/

publicinterface Login {

/**

* 登录服务

* @return

*/

public String login(String username,String password);

}

【第2步】 编写客户端测试类suning.client.SimpleTest.java

package suning.client;

import java.net.MalformedURLException;

import com.caucho.hessian.client.HessianProxyFactory;

import suning.server.Login;

/**

* 客户端测试[Java Project]

*

* @author 莫小哆_ly 2012-1-16

*/

publicclass SimpleTest {

publicstaticvoid main(String[] args) {

// 2. 未用Spring配置

String url = “http://10.21.140.47:8080/myHessianSpringServer/remoting/loginService”;

HessianProxyFactory factory = new HessianProxyFactory();

Login loginOpera;

try {

loginOpera = (Login) factory.create(Login.class, url);

// 直接调用接口方法,实际通过服务器端执行实现类方法,返回结果

System.out.println(“远程调用:” + loginOpera.login(“1″, “admin”));

} catch (MalformedURLException e) {

e.printStackTrace();

}

// 3. 未用Spring配置

url = “http://10.21.140.47:8080/myHessianSpringServer/login”;

factory = new HessianProxyFactory();

try {

loginOpera = (Login) factory.create(Login.class, url);

// 直接调用接口方法,实际通过服务器端执行实现类方法,返回结果

System.out.println(“远程调用:” + loginOpera.login(“admin”, “admin”));

} catch (MalformedURLException e) {

e.printStackTrace();

}

}

}

3 创建客户端,同样注入Spring

【Dynamic Web Project】

导入jar包

【第1步】 创建客户端接口suning.server.Login.java

package suning.server;

/**

*

* hessian 服务接口,用于客户端调用

* Hessian建立的远程调用,是对服务的接口进行调用

*

* @author 莫小哆_ly 2012-1-16

*/

publicinterface Login {

/**

* 登录服务

* @return

*/

public String login(String username,String password);

}

【第2步】 创建Spring配置文件suning.resource.bean.xml

<?xmlversion=”1.0″encoding=”UTF-8″?>

<beansxmlns=”http://www.springframework.org/schema/beans”

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”xmlns:context=”http://www.springframework.org/schema/context”

xmlns:tx=”http://www.springframework.org/schema/tx”xmlns:aop=”http://www.springframework.org/schema/aop”

xsi:schemaLocation=”http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-2.5.xsd”>

<beanid=”myHessianSpringClient”

class=”org.springframework.remoting.caucho.HessianProxyFactoryBean”>

<!– 本地调用服务URL –>

<propertyname=”serviceUrl”>

<value>http://10.21.140.47:8080/myHessianSpringServer/remoting/loginService

</value>

</property>

<!– 本地接口路径 –>

<propertyname=”serviceInterface”>

<value>suning.server.Login</value>

</property>

</bean>

</beans>

【第3步】 编写Web项目配置文件WEB-INF/web.xml

<?xmlversion=”1.0″encoding=”UTF-8″?>

<web-appxmlns: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”

id=”WebApp_ID”version=”2.5″>

<display-name>hessianSpringClient</display-name>

<!– 设置Spring容器加载配置文件路径 –>

<context-param>

<param-name>contextConfigLocation</param-name>

<!– /com/resources/applicationContext.xml –>

<param-value>classpath:/suning/resource/bean.xml</param-value>

</context-param>

<!–Spring ApplicationContext 载入 –>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

</web-app>

【第4步】 编写客户端测试类suning.client.SimpleTest.java

package suning.client;

import java.net.MalformedURLException;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.caucho.hessian.client.HessianProxyFactory;

import suning.server.Login;

/**

* 客户端测试[Web Project]

*

* @author 莫小哆_ly 2012-1-16

*/

publicclass SimpleTest {

publicstaticvoid main(String[] args) {

// 1. 服务器端注入Spring

// 1.1 客户端注入Spring

// 分析Spring配置文档

ApplicationContext appContext = new ClassPathXmlApplicationContext(

“suning/resource/bean.xml”);

// 获取配置的bean – bean.xml

Login loginSpringOpera = (Login) appContext.getBean(“myHessianSpringClient”);

// 直接调用接口方法,实际通过服务器端执行实现类方法,返回结果

System.out.println(“远程调用:” + loginSpringOpera.login(“admin”, “admin”));

// 1.2 客户端未注入Spring[可以创建Java Project]

String url = “http://10.21.140.47:8080/myHessianSpringServer/remoting/loginService”;

HessianProxyFactory factory = new HessianProxyFactory();

Login loginOpera;

try {

loginOpera = (Login) factory.create(Login.class, url);

// 直接调用接口方法,实际通过服务器端执行实现类方法,返回结果

System.out.println(“远程调用:” + loginOpera.login(“1″, “admin”));

} catch (MalformedURLException e) {

e.printStackTrace();

}

// 2. 服务器端及客户端都未注入Spring

url = “http://10.21.140.47:8080/myHessianSpringServer/login”;

factory = new HessianProxyFactory();

try {

loginOpera = (Login) factory.create(Login.class, url);

// 直接调用接口方法,实际通过服务器端执行实现类方法,返回结果

System.out.println(“远程调用:” + loginOpera.login(“admin”, “admin”));

} catch (MalformedURLException e) {

e.printStackTrace();

}

}

}

4 运行

run as Server — hessianSpringServer

run as Server — hessianSpringClient(客户端为Java Project时,不需要运行)

run as Java Aplication — hessianSpringClient /suning/client/SimpleTest.java



快乐成长 每天进步一点点