注意事项

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

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

 <?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/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项参数与log4j的配置相关 -->
<!-- start -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param> <context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
<!-- end --> <!-- 一个web.xml中可以配置多个DispatcherServlet,通过 servlet-mapping的不同设置,让每个DispatcherServlet处理不同的请求--> <!-- 业务层和持久层的bean的spring配置文件。applicationContext.xml.多个配置文件使用,号隔开-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring-mybatis/spring-mybatis.xml</param-value>
</context-param> <!-- 配置Spring监听 。通过contextConfigLocation配置的xml文件启动业务层(service和dao)的bean的容器。【service层和dao层的容器】-->
<!-- spring的监听器 -->
<listener>
<description>spring监听器</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 暴露hessian接口的servlet -->
<servlet>
<servlet-name>hessian</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 此处加载的hessin-servlet.xml必须是该格式【servlet的名字】-servlet.xml。如果非该格式,报错 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/hessian-remote/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> </web-app>

【2】hessian的接受器配置。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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <!-- 主体的扫描除controller外的所有组件 -->
<context:component-scan base-package="org.paymoney.*" >
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="1234"></property>
</bean> <!-- 配置事务管理器 -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> <!-- 注解方式配置事物 proxy-target-class="true" 值为true时选用cglib动态代理,事务注解放置具体类的方法上, 值为false为jdk动态代理管理事务,事务注解放置接口方法上 -->
<tx:annotation-driven transaction-manager="transactionManager" /> <!--注解风格支持,当带事务注解的业务类中方法自调用时,为了防止事务失效-->
<aop:aspectj-autoproxy expose-proxy="true"/> <!-- mybatis文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据源。数据库连接 -->
<property name="dataSource" ref="dataSource" />
<!-- 扫描该包下的类,给每一个javaBean起一个别名,别名是首字母小写的javaBean类名 -->
<property name="typeAliasesPackage" value="org.paymoney.comment" />
<!-- 自动扫描entity目录,省略Configuration.xml里手工配置 -->
<property name="mapperLocations" value="classpath*:/org/paymoney/mapper/*.xml" />
<!-- 暂定不知道该配置对不 -->
<!-- <bean id="sqlSession"class="org.mybatis.spring.SqlSessionTemplate"> -->
</bean> <!-- 接口实例化管理的bean -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="org.paymoney.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
</beans>

【4】暴漏的接口实现类

 import java.util.Date;

 import javax.annotation.Resource;

 import org.paymoney.comment.Order;
import org.paymoney.dao.OrderMapper;
import org.paymoney.port.OrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; @Service(value="orderService")
public class OrderServiceImpl implements OrderService{ @Resource
private OrderMapper orderMapper; //说你好
@Override
@Transactional(propagation=Propagation.REQUIRED)
public void sayHello(String name, Integer age, Date brithday) {
// TODO Auto-generated method stub
System.out.println("OrderServiceImpl.sayHello():姓名--->"+name+" 年龄--->"+age+" 生日-->"+brithday.toString()); } //添加一个订单
@Override
@Transactional(propagation=Propagation.REQUIRED)
public void addOrder(Order order) {
System.out.println("OrderServiceImpl.addOrder(添加订单前)");
orderMapper.addOrder(order);
System.out.println("OrderServiceImpl.addOrder(添加订单后)"); } }

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

 <?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/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>json_test</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list> <!-- 一个web.xml中可以配置多个DispatcherServlet,通过 servlet-mapping的不同设置,让每个DispatcherServlet处理不同的请求--> <!-- 业务层和持久层的bean的spring配置文件。applicationContext.xml.多个配置文件使用,号隔开
此处加载spring-hessianclient.xml
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring-*.xml</param-value>
</context-param> <!-- 配置Spring监听 。通过contextConfigLocation配置的xml文件启动业务层(service和dao)的bean的容器。【service层和dao层的容器】-->
<!-- spring的监听器 -->
<listener>
<description>spring监听器</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- Spring的log4j监听器 -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- 防止spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener> <!-- 配置SpringMVC的DispatcherServlet ,它是springmvc的灵魂和心脏,它协调各组件完成一次完整的请求响应-->
<!-- (默认自动加载web-inf下的<servltname>-servlet.xml的spring配置文件)启动web层的spring容器【控制器,请求分发器】 -->
<!-- 如果配置init-param则是打破默认自动加载,而是按param-value中的路径,加载web层容器 -->
<!-- web层的spring容器是业务层的spring容器的子容器:即web层容器中的bean【controller】可以调用业务层bean【service和dao】而业务层bean调用不到web层的bean -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 装配webApplicationContext容器。其实ApplicationContext容器的子类-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <!-- 配置字符集 -->
<filter>
<filter-name>encodingFilter</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>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> </web-app>

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

   <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 客户端Hessian代理工厂Bean -->
<bean id="hessianFactory" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<!-- 请求代理Servlet路径 -->
<property name="serviceUrl">
<!-- 需要请求暴漏接口的服务地址。http://ip/服务应用名/hessian的servlet名字/请求的标示 -->
<value>http://localhost:8081/paymoney-hessian/hessian/hessianOrder</value>
</property>
<!-- 接口定义 -->
<property name="serviceInterface">
<value>org.paymoney.port.OrderService</value>
</property>
</bean>
</beans>

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

 /**
* 测试hessian
*/
public void testHessian() {
// TODO Auto-generated method stub
//从applicationContext容器中获取hessianfacttory
OrderService orderService=(OrderService) context.getBean("hessianFactory"); Date aDate=new Date();
//调用远程方法
orderService.sayHello("sxf", 25,aDate);
Order order=new Order();
order.setOrderNum(1234);
order.setCompanyName("易宝");
order.setPersonName("尚晓飞");
order.setOrderTime(aDate);
//调用远程方法
orderService.addOrder(order);
System.out.println("AuthorServiceImpl.testHessian()"); }

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

 public static void main(String[] args) throws MalformedURLException {
//远程调用路径
String url = "http://localhost:8081/paymoney-hessian/hessian/hessianOrder";
//hessianproxyFactory的工厂对象。
HessianProxyFactory factory = new HessianProxyFactory();
//获取远程调用的接口代理对象
OrderService orderService = (OrderService) factory.create(OrderService.class, url); Date gDate=new Date();
//调用远程方法
orderService.sayHello("123", 25,gDate); Order order=new Order();
order.setOrderNum(5201314);
order.setCompanyName("天天向上");
order.setOrderTime(gDate);
order.setPersonName("尚晓飞");
//调用远程方法
orderService.addOrder(order); }

最新文章

  1. jboss设置图片上传大小
  2. ListView的item里面控件文本颜色修改
  3. CSS:z-index层级在IE中无效
  4. 更新新网卡驱动,修复win7雷凌网卡Ralink RT3290在电脑睡眠时和启动网卡时出现蓝屏netr28x.sys驱动文件错误
  5. docker加速器
  6. LitDB笔记
  7. Windows Live Writer配置测试
  8. 【hdu2896】病毒侵袭
  9. 安装完oracle重新启动后报ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务(重启前正常)
  10. JavaScript String 对象实例深入研究
  11. 一个简单的mfc单页界面文件读写程序(MFC 程序入口和执行流程)
  12. 【iOS】苹果,百度Map定位使用与总结
  13. Lucene.net(4.8.0) 学习问题记录五: JIEba分词和Lucene的结合,以及对分词器的思考
  14. Scrollbar
  15. dom4j 解析 xml标签属性
  16. cf161d 求距离为k的点对(点分治,树形dp)
  17. 阿里云提示WordPress“/wp-includes/http.php输入IP验证不当”的解决办法
  18. Android Launcher分析和修改12——Widget列表信息收集
  19. yii基础控制器安全验证
  20. es6比es5节省代码的地方总结

热门文章

  1. JavaScript权威指南--类型、值和变量
  2. 英语词根与单词的说文解字---词根示例1、第10页 st(at)
  3. 在xampp集成环境下使用 php 连接oracle
  4. CSS3:@font-face规则
  5. http code码实验
  6. mysql中一个字段根据另一字段的值分割为不同列
  7. bzoj4945
  8. python爬取商品信息
  9. TCP三次握手,四次挥手,状态变迁图
  10. gzip压缩解压缩