Spring整合Hessian
公司做的项目是移动互联网的App产品,采用的是分布式开发架构,总共分为三层,移动端+应用开发平台+基础开发平台。简单介绍下:
移动端:Android,Ios,涉及部分html5
应用开发平台:处理相关业务逻辑,整合平台 提供的各种平台接口的数据,起个聚合的作用。 采用的框架式 SSH(strtus2+spring3+Hessian)
基础开发平台: 注重功能的实现,不涉及业务处理,提供标准的规范的接口与数据库交互,采用的框架是SSI(
springMVC+Spring+Ibatis)
其中应用开发平台集成得到是相关基础开发平台的接口,数据交互使用的就是Hession。关于Hessian的介绍请在百度下很多介绍,原理分析请看http://blog.csdn.net/zhtang0526/article/details/4788879分享的资源。
准备工作:
下载Spring3.2.4
Logger版本:log4j-1.2.17.jar
Hessian版本:hessian-3.1.3.jar
开发工具:eclipse Tomcat
第一步:创建一个动态web项目 Z-Up(可以随意制定,只要符合规范就行),并导入下载的jar包
第二步:配置日志,我这里面涉及日志的配置,详见上篇中介绍,这主要讲Hession的配置
第三步:在包目录下com.syswin.hessian下创建三个类代码如下:
package com.syswin.hessian;
import java.io.Serializable;
/**
* @author HLY
* @date 创建时间:2014年11月24日 下午3:20:47
*/
public class Person implements Serializable{
private String name;
private Integer age;
private Long id;
public Person(Long id,Integer age,String name){
this.name = name;
this.age = age;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}package com.syswin.hessian;
/**
* @author HL Y
* @date 创建时间:2014年11月24日 下午3:19:25
*/
public interface HessianInterface {
public Person getPerson();
public String removePerson();
public Long addPerson();
}package com.syswin.hessian;
import org.apache.log4j.Logger;
/**
* @author HL Y
* @date 创建时间:2014年11月24日 下午3:17:01
*/
public class HessianInterfaceImpl implements HessianInterface{
public static Logger log = Logger.getLogger(HessianInterfaceImpl.class);
public Person getPerson() {
return new Person(1L,14,"Wang Mei");
}
public String removePerson() {
return "success";
}
public Long addPerson() {
return new Person(1L,14,"Wang Mei").getId();
}
}第四步:类路径下创建applicationContext.xml并注入PersonService接口
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<bean id="hessianServiceImpl" class="com.syswin.hessian.HessianInterfaceImpl"></bean>
</beans>第五步:在类路径下出创建hessian-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" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> <!-- Hessian远程服务 --> <bean name="/hessianServiceCall" class="org.springframework.remoting.caucho.HessianServiceExporter"> <property name="service" ref="hessianServiceImpl" /> <property name="serviceInterface" value="com.syswin.hessian.HessianInterface" /> </bean> </beans>
第六步:在web.xml中配置我的包含日志的配置,不需要的可以删除掉就ok
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<!--如果不定义webAppRootKey参数,那么webAppRootKey就是缺省的"webapp.root"-->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>Z-Up.root</param-value>
</context-param>
<!-- log4j.properties 的路径 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<!-- 开一条watchdog线程每60秒扫描一下配置文件的变化 -->
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>1000</param-value>
</context-param>
<!-- log4j的日志监听-->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
<listener>
<!-- 在Spring2.0中除了以前的Singleton和Prototype外又加入了三个新的web作用域,分别为request、session和global session,如果你想让你的容器里的某个bean拥有其中某种新的web作用域,除了在bean级上配置相应的scope属性,还必须在容器级做一个额外的初始化配置 -->
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter>
<filter-name>CharacterEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<servlet>
<servlet-name>hessian</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:hessian-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hessian</servlet-name>
<url-pattern>/hessian/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>配置中红色部分为spring容器加载的配置,Hessian的配置是蓝色字体,其实就是声明的一个大家熟悉的servlet,当然路径可根据实际路径调整。
注意事项:
a)hessian-servlet.xml的文件名必须以<servlet-name>hessian</servlet-name>名字开头,并且加上-servlet.xml一段,组成完整的文件名。
b)hessian-servlet.xml的文件名格式必须是[servlet-name]-servlet.xml格式,否则出错。
因为在配置中没太注意约定的配置,话费了点时间。
到此为止整合完成,我们进行最后一步测试
第六步测试Hessian的两种调用方式:
在com.syswin.junit包下创建一个TestCase文件名为JunitTest1.java
package com.syswin.junit;
import java.net.MalformedURLException;
import org.junit.BeforeClass;
import org.junit.Test;
import com.caucho.hessian.client.HessianProxyFactory;
import com.syswin.hessian.HessianInterface;
import com.syswin.hessian.Person;
/**
* @author HL Y
* @date 创建时间:2014年11月25日 下午1:25:03
*/
public class JunitTest1 {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test
public void test() throws MalformedURLException, ClassNotFoundException {
System.out.println("hessian call --------");
String url = "http://172.31.65.50:8080/Z-Up/hessian/hessianServiceCall";
HessianProxyFactory factory = new HessianProxyFactory();
HessianInterface service = (HessianInterface) factory.create(url); factory.create(HessianInterface.class, url);
Person person = service.getPerson();
System.out.println(" ======== " + service.getPerson().getName());
System.out.println("-----success " + person.getId());
}
}解释下方为路径:
http://172.31.65.50:8080/Z-Up/hessian/hessianServiceCall
http://协议
172.31.65.50本机IP可为localhost
8080是Tomcat端口号
Z-UP为项目的名称
hessian是在web.xml配置的servlet中配置的访问路径
hessianServiceCall为在hessian-servlet.xml中配置的访问的接口注意写法是“/hessianServiceCall”
启动Tomcat服务,现在相当于应用提供了Hessian服务,咱们的Main方法相当于客户端做测试代码如下:
package com.syswin.junit;
import java.net.MalformedURLException;
import org.junit.BeforeClass;
import org.junit.Test;
import com.caucho.hessian.client.HessianProxyFactory;
import com.syswin.hessian.HessianInterface;
import com.syswin.hessian.Person;
/**
* @author HL Y
* @date 创建时间:2014年11月25日 下午1:25:03
*/
public class JunitTest1 {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test
public void test() throws MalformedURLException, ClassNotFoundException {
System.out.println("hessian call --------");
String url = "http://172.31.65.50:8080/Z-Up/hessian/hessianServiceCall";
HessianProxyFactory factory = new HessianProxyFactory();
HessianInterface service = (HessianInterface) factory.create(url); factory.create(HessianInterface.class, url);
Person person = service.getPerson();
System.out.println(" ======== " + service.getPerson().getName());
System.out.println("-----success " + person.getId());
}
}然后在控制台打印出:表示配置 success
hessian call -------- ======== Wang Mei -----success 1
好再看另一种测试方法:代码如下
package com.syswin.junit;
import org.apache.log4j.Logger;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.syswin.hessian.HessianInterface;
import com.syswin.hessian.Person;
/**
* @author HL Y
* @date 创建时间:2014年11月25日 下午1:25:24
*/
public class JunitTest2 {
private static Logger log = Logger.getLogger(JunitTest2.class);
private static ApplicationContext ctx ;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
}
@Test
public void test() {
HessianInterface personService = (HessianInterface) ctx.getBean("personService");
Person p = personService.getPerson();
System.out.println(" name is " + p.getName() + " age is " + p.getAge());
}
}同样控制台打印出:
name is Wang Mei age is 14
第一种方式是基于Hessian代理的方式访问测试
第二种基本spring注入的方式访问相当于spring做有一层封装。
初次写博文,若有写的不清楚,或者遗漏之处请大家指出来,希望和大家一块学习交流。