Spring集成Hessian
一、概述
Spring 通过org.springframework.remoting.caucho.HessianServiceExporter将POJO中的所有public方法发布为Hessian服务。
二、实例(服务端)
1、编写服务接口
1 package com.cnblogs.javalouvre.service; 2 3 public interface GreetService { 4 5 String sayHello(String name); 6 7 }
2、编写服务实现
1 package com.cnblogs.javalouvre.service; 2 3 import com.caucho.hessian.server.HessianServlet; 4 5 public class GreetServiceImpl extends HessianServlet implements GreetService { 6 7 private static final long serialVersionUID = 1880738686281295739L; 8 9 @Override 10 public String sayHello(String name) { 11 return "Hello " + name; 12 } 13 14 }
3、导出Hessian服务
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> 6 7 <bean id="greetService" class="com.cnblogs.javalouvre.service.GreetServiceImpl" /> 8 9 <bean name="/GreetService" class="org.springframework.remoting.caucho.HessianServiceExporter"> 10 <property name="service" ref="greetService" /> 11 <property name="serviceInterface" value="com.cnblogs.javalouvre.service.GreetService" /> 12 </bean> 13 14 </beans>
4、配置Hessian控制器
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://java.sun.com/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 5 version="2.5"> 6 7 <servlet> 8 <servlet-name>remoting</servlet-name> 9 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 10 <load-on-startup>1</load-on-startup> 11 </servlet> 12 <servlet-mapping> 13 <servlet-name>remoting</servlet-name> 14 <url-pattern>/remoting/*</url-pattern> 15 </servlet-mapping> 16 17 <welcome-file-list> 18 <welcome-file>index.html</welcome-file> 19 <welcome-file>index.htm</welcome-file> 20 <welcome-file>index.jsp</welcome-file> 21 </welcome-file-list> 22 23 </web-app>
三、示例(客户端)
1、配置声明
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> 6 7 <bean id="greetService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean"> 8 <property name="serviceUrl" value="http://10.108.1.138:8080/remoting/GreetService"/> 9 <property name="serviceInterface" value="com.cnblogs.javalouvre.service.GreetService"/> 10 </bean> 11 12 </beans>
2、调用方法
1 package com.cnblogs.javalouvre.client; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 import com.cnblogs.javalouvre.service.GreetService; 7 8 public class Client { 9 10 public static void main(String[] args) { 11 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); 12 GreetService service = context.getBean("greetService", GreetService.class); 13 System.out.println(service.sayHello("Jobs")); 14 } 15 16 }
-----------------------------------------------------------------------------------------------------------
薔薇猛虎皆成個性,陽光雨露俱是天恩!
薔薇猛虎皆成個性,陽光雨露俱是天恩!
浙公网安备 33010602011771号