一、简介
这里我们介绍hessian与spring的整合,包含服务端和客户端
二、服务端开发步骤
1、添加maven依赖
<dependency> <groupId>com.caucho</groupId> <artifactId>hessian</artifactId> <version>4.0.7</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.2.9.RELEASE</version> </dependency>2、添加服务接口和相应实现类
public interface HelloService { String sayHello(String name); }
@Service("helloService") public class HelloServiceImpl implements HelloService{ public String sayHello(String name) { return "hello,"+name; } }3、在spring中配置服务接口、实现类以及访问地址
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd "> <context:annotation-config /> <context:component-scan base-package="com.dragon.hessianServer" /> <bean name="/helloService" class="org.springframework.remoting.caucho.HessianServiceExporter"> <property name="service" ref="helloService"/> <property name="serviceInterface"> <value>com.dragon.hessianServer.service.HelloService</value> </property> </bean> </beans>
4、在web.xml配置springmvc的servlet
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <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>hessian</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>hessian</servlet-name> <url-pattern>/hessian/*</url-pattern> </servlet-mapping> </web-app>这样, hessian和spring整合的服务配置完成,启动web项目即可开启服务。
三、客户端开发步骤
1、添加maven依赖
<dependency> <groupId>com.caucho</groupId> <artifactId>hessian</artifactId> <version>4.0.7</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.9.RELEASE</version> </dependency>2、添加自定义的hessian服务接口jar包(本例即是包含HelloService的包)
3、在spring中配置接口服务bean,主要包含访问地址,服务接口
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd "> <context:annotation-config /> <bean id="helloService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean"> <property name="serviceUrl" value="http://localhost:8080/hessian/helloService"/> <property name="serviceInterface" value="com.dragon.hessianServer.service.HelloService"/> </bean> </beans>4、服务调用测试
public class HessianSpringClient { public static void main(String[] args) throws MalformedURLException { ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); HelloService helloService = (HelloService)ctx.getBean("helloService"); System.out.println(helloService.sayHello("apple")); } }输出结果:
hello,apple