首页 > 代码库 > hessian+spring集成应用

hessian+spring集成应用

注意事项

▲JAVA服务器端必须具备以下几点:
---->包含Hessian的jar包
---->设计一个接口,用来给客户端调用
---->实现该接口的功能
---->配置web.xml,配好相应的servlet
---->对象必须实现Serializable 接口
---->对于复杂对象可以使用Map的方法传递

▲客户端必须具备以下几点:
---->java客户端包含Hessian.jar的包。C#中引用hessianCSharp.dll
---->具有和服务器端结构一样的接口。包括命名空间都最好一样
---->利用HessianProxyFactory调用远程接口。
服务器端(向外暴漏接口的应用)
【1】配置该web应用的web.xml文件

技术分享
 1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns: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"> 3      <!-- 以下3项参数与log4j的配置相关 --> 4      <!-- start -->            5     <context-param>      6         <param-name>log4jConfigLocation</param-name>      7         <param-value>/WEB-INF/log4j.properties</param-value>      8     </context-param>      9           10     <context-param>     11         <param-name>log4jRefreshInterval</param-name>     12         <param-value>60000</param-value>     13     </context-param>     14     <listener>     15         <listener-class>     16              org.springframework.web.util.Log4jConfigListener      17         </listener-class>     18     </listener>     19     <!-- end -->     20   21   <!-- 一个web.xml中可以配置多个DispatcherServlet,通过 servlet-mapping的不同设置,让每个DispatcherServlet处理不同的请求-->22   23   <!-- 业务层和持久层的bean的spring配置文件。applicationContext.xml.多个配置文件使用,号隔开-->24   <context-param>25       <param-name>contextConfigLocation</param-name>26       <param-value>classpath:/spring-mybatis/spring-mybatis.xml</param-value>27   </context-param>28   29   <!-- 配置Spring监听 。通过contextConfigLocation配置的xml文件启动业务层(service和dao)的bean的容器。【service层和dao层的容器】-->30   <!-- spring的监听器 -->31     <listener>32         <description>spring监听器</description>33         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>34     </listener>35 36 37   <!-- 暴露hessian接口的servlet -->38   <servlet>39         <servlet-name>hessian</servlet-name>40         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>41         <!-- 此处加载的hessin-servlet.xml必须是该格式【servlet的名字】-servlet.xml。如果非该格式,报错 -->42         <init-param>  43             <param-name>contextConfigLocation</param-name>  44             <param-value>classpath:/hessian-remote/hessian-servlet.xml</param-value>  45         </init-param>  46         <load-on-startup>1</load-on-startup>47     </servlet>48 49     <servlet-mapping>50         <servlet-name>hessian</servlet-name>51         <url-pattern>/hessian/*</url-pattern>52     </servlet-mapping>53   54 55   </web-app>
View Code

【2】hessian的接受器配置。hessian-servlet.xml的配置

技术分享
 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"  3 xmlns:context="http://www.springframework.org/schema/context" 4 xsi:schemaLocation=" 5 http://www.springframework.org/schema/beans  6 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  7 http://www.springframework.org/schema/tx  8 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 9 http://www.springframework.org/schema/aop 10 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd11 http://www.springframework.org/schema/context12 http://www.springframework.org/schema/context/spring-context-3.2.xsd">13 14     <!-- 主体的扫描除controller外的所有组件  -->15     <context:component-scan base-package="org.paymoney.*" >16         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>17         <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>18     </context:component-scan>19     <!-- 配置数据源 -->20     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >21         <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>22         <property name="url" value="jdbc:mysql://localhost:3306/test"></property>23         <property name="username" value="root"></property>24         <property name="password" value="1234"></property>25     </bean>26         27         28     <!-- 配置事务管理器 -->29     <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">30         <property name="dataSource" ref="dataSource" />31     </bean>32     33     34     <!-- 注解方式配置事物  proxy-target-class="true" 值为true时选用cglib动态代理,事务注解放置具体类的方法上,  值为false为jdk动态代理管理事务,事务注解放置接口方法上 -->35      <tx:annotation-driven transaction-manager="transactionManager"  />36     37     <!--注解风格支持,当带事务注解的业务类中方法自调用时,为了防止事务失效-->  38     <aop:aspectj-autoproxy expose-proxy="true"/>39     40     <!-- mybatis文件 -->41     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">42         <!-- 数据源。数据库连接 -->43         <property name="dataSource" ref="dataSource" />44         <!-- 扫描该包下的类,给每一个javaBean起一个别名,别名是首字母小写的javaBean类名 -->45         <property name="typeAliasesPackage" value="org.paymoney.comment" />46         <!-- 自动扫描entity目录,省略Configuration.xml里手工配置 -->47         <property name="mapperLocations" value="classpath*:/org/paymoney/mapper/*.xml" />48         <!-- 暂定不知道该配置对不 -->49         <!-- <bean id="sqlSession"class="org.mybatis.spring.SqlSessionTemplate"> -->50     </bean>51 52     <!-- 接口实例化管理的bean -->53     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">54         <property name="basePackage" value="org.paymoney.dao" />55         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />56     </bean>57 </beans>
View Code

【4】暴漏的接口实现类

技术分享
 1 import java.util.Date; 2  3 import javax.annotation.Resource; 4  5 import org.paymoney.comment.Order; 6 import org.paymoney.dao.OrderMapper; 7 import org.paymoney.port.OrderService; 8 import org.springframework.stereotype.Service; 9 import org.springframework.transaction.annotation.Propagation;10 import org.springframework.transaction.annotation.Transactional;11 12 @Service(value="http://www.mamicode.com/orderService")13 public class OrderServiceImpl implements OrderService{14     15     @Resource16     private OrderMapper orderMapper;17     18     //说你好19     @Override20     @Transactional(propagation=Propagation.REQUIRED)21     public void sayHello(String name, Integer age, Date brithday) {22         // TODO Auto-generated method stub23         System.out.println("OrderServiceImpl.sayHello():姓名--->"+name+"  年龄--->"+age+"  生日-->"+brithday.toString());24         25     }26     27     //添加一个订单28     @Override29     @Transactional(propagation=Propagation.REQUIRED)30     public void addOrder(Order order) {31         System.out.println("OrderServiceImpl.addOrder(添加订单前)");32         orderMapper.addOrder(order);33         System.out.println("OrderServiceImpl.addOrder(添加订单后)");34         35     }36 37     38 }
View Code

 

客户端(需要调用另一个应用接口的项目)
【1】配置该web应用的web.xml文件

技术分享
 1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns: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"> 3   <display-name>json_test</display-name> 4   <welcome-file-list> 5     <welcome-file>login.jsp</welcome-file> 6   </welcome-file-list> 7   8   <!-- 一个web.xml中可以配置多个DispatcherServlet,通过 servlet-mapping的不同设置,让每个DispatcherServlet处理不同的请求--> 9   10   <!-- 业务层和持久层的bean的spring配置文件。applicationContext.xml.多个配置文件使用,号隔开11     此处加载spring-hessianclient.xml12 -->13   <context-param>14       <param-name>contextConfigLocation</param-name>15       <param-value>classpath*:/spring-*.xml</param-value>16   </context-param>17   18   <!-- 配置Spring监听 。通过contextConfigLocation配置的xml文件启动业务层(service和dao)的bean的容器。【service层和dao层的容器】-->19   <!-- spring的监听器 -->20     <listener>21         <description>spring监听器</description>22         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>23     </listener>24 25 26     <!-- Spring的log4j监听器 -->  27     <listener>  28         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  29     </listener>  30     <!-- 防止spring内存溢出监听器 -->31     <listener>32         <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>33     </listener>34   35   <!-- 配置SpringMVC的DispatcherServlet ,它是springmvc的灵魂和心脏,它协调各组件完成一次完整的请求响应-->36   <!-- (默认自动加载web-inf下的<servltname>-servlet.xml的spring配置文件)启动web层的spring容器【控制器,请求分发器】 -->37   <!-- 如果配置init-param则是打破默认自动加载,而是按param-value中的路径,加载web层容器 -->38   <!-- web层的spring容器是业务层的spring容器的子容器:即web层容器中的bean【controller】可以调用业务层bean【service和dao】而业务层bean调用不到web层的bean -->39   <servlet>40       <servlet-name>springMVC</servlet-name>41       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>42       <init-param>43       <!-- 装配webApplicationContext容器。其实ApplicationContext容器的子类-->44           <param-name>contextConfigLocation</param-name>45           <param-value>classpath*:/spring-mvc.xml</param-value>46       </init-param>47       <load-on-startup>1</load-on-startup>48   </servlet>49   <servlet-mapping>50       <servlet-name>springMVC</servlet-name>51       <url-pattern>/</url-pattern>52   </servlet-mapping>53   54   <!-- 配置字符集 -->55   <filter>56       <filter-name>encodingFilter</filter-name>57       <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>58       <init-param>59           <param-name>encoding</param-name>60           <param-value>UTF-8</param-value>61       </init-param>62       <init-param>63           <param-name>forceEncoding</param-name>64           <param-value>true</param-value>65       </init-param>66   </filter>67   <filter-mapping>68       <filter-name>encodingFilter</filter-name>69       <url-pattern>/*</url-pattern>70   </filter-mapping>71    72 </web-app>
View Code

【2】客户端需要配置的spring-hessianclient.xml

技术分享
 1   <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">   2     <beans>   3         <!-- 客户端Hessian代理工厂Bean -->   4         <bean id="hessianFactory" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">   5             <!-- 请求代理Servlet路径 -->           6             <property name="serviceUrl">   7                 <!-- 需要请求暴漏接口的服务地址。http://ip/服务应用名/hessian的servlet名字/请求的标示 --> 8                <value>http://localhost:8081/paymoney-hessian/hessian/hessianOrder</value>   9             </property>  10             <!-- 接口定义 -->  11             <property name="serviceInterface">  12                 <value>org.paymoney.port.OrderService</value>  13             </property>  14         </bean>  15     </beans> 
View Code

【3】客户端业务类中的远程调用接口

技术分享
 1 /** 2      * 测试hessian 3      */ 4     public void testHessian() { 5         // TODO Auto-generated method stub 6         //从applicationContext容器中获取hessianfacttory 7         OrderService orderService=(OrderService) context.getBean("hessianFactory"); 8          9         Date aDate=new Date();10         //调用远程方法11         orderService.sayHello("sxf", 25,aDate);12         Order order=new Order();13         order.setOrderNum(1234);14         order.setCompanyName("易宝");15         order.setPersonName("尚晓飞");16         order.setOrderTime(aDate);17         //调用远程方法18         orderService.addOrder(order);19         System.out.println("AuthorServiceImpl.testHessian()");20         21     }
View Code

【4】也可以通过另一种方式调用远程方法。

技术分享
 1 public static void main(String[] args) throws MalformedURLException { 2         //远程调用路径 3         String url = "http://localhost:8081/paymoney-hessian/hessian/hessianOrder"; 4         //hessianproxyFactory的工厂对象。 5         HessianProxyFactory factory = new HessianProxyFactory(); 6         //获取远程调用的接口代理对象 7         OrderService orderService = (OrderService) factory.create(OrderService.class, url); 8          9         Date gDate=new Date();10         //调用远程方法11         orderService.sayHello("123", 25,gDate);12         13         Order order=new Order();14         order.setOrderNum(5201314);15         order.setCompanyName("天天向上");16         order.setOrderTime(gDate);17         order.setPersonName("尚晓飞");18         //调用远程方法19         orderService.addOrder(order);20        21     }
View Code

 



hessian+spring集成应用