hessian与spring的完美结合(使用spring注解自动注入bean)
2014-12-20 12:43
816 查看
1.什么是Hessian
Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能.相比WebService,Hessian更简单、快捷。<
xmlnamespace prefix ="o" ns ="urn:schemas-microsoft-com:office:office" />
我们可以从hessian的定义中得到两个信息:
1) hessian主要功能是提供RMI功能。
2) hessian和webservice很像,他比较好学。
2.什么是RMI
RMI(Remote Method Invocation)是远程方法的简称,他能够帮我们查找并执行远程对象的方法。通俗的说,远程调用就象将一个class放在A机器上,然后在B机器中调用这个class的方法。
其他机器需要调用的对象必须被导出到远程注册服务器,这样才能被其他机器调用。因此,如果机器A要调用机器B上的方法,则机器B必须将该对象导出到其远程注册服务器。注册服务器是服务器上运行的一种服务,它帮助客户端远程地查找和访问服务器上的对象。一个对象只有导出来后,然后才能实现RMI包中的远程接口。例如,如果想使机器A中的Xyz对象能够被远程调用,它就必须实现远程接口。
3.配置hessian服务端
配置java服务端,需要满足以下条件:
1)包含hessian的jar包,spring jar包,spring-mvc
jar包
2)设计一个接口,用来给客户端调用
3)实现该接口的功能
4)配置web.xml(配置servlet)
5)对象必须实现Serializable接口
6)配置mvc配置文件和hessian配置文件
实现部分:
1)web.xml文件中
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<!-- 配置sruts2的中央控制器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<!-- 配置spring的监听器,加载Spring配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring/applicationContext-*.xml</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<!-- 开启监听 -->
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- hessian 定义spring的调试分配器 -->
<servlet>
<servlet-name>remoting</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring/remoting-servlet.xml,classpath:/spring/mvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>remoting</servlet-name>
<url-pattern>/remoting/*</url-pattern>
</servlet-mapping>
<!-- 自定义的监听类-->
<listener>
<listener-class>com.hxrainbow.crm.util.InitListener</listener-class>
</listener>
<!-- session超时定义,单位为分钟 ,默认为30分钟-->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<!-- 页面没找到 -->
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
<!-- 内部服务器错误 -->
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
</web-app>
配置文件中需要注意struts2过滤器部分的配置,因为我们发布的hessian接口没有后缀名,所以在访问的时候我们得做处理(修改配置文件),以免误以为访问struts2。还有就是注意spring的配置要在hessian的配置之前,因为hessian要使用spring中实例化的东西。
2)mvc-config.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- 启动springmvc的注解映射功能 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="1" />
</bean>
<!-- Hessian 启动BeanNameMapping 映射功能 -->
<bean
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="defaultHandler" ref="httpRequestHandlerAdapter" />
<property name="order" value="2" />
</bean>
<!-- 启动springmvc的注解功能 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!-- 处理httpRequest的Adapter
-->
<bean id="httpRequestHandlerAdapter"
class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean>
<!-- spring 对 com 进行扫描 -->
<context:component-scan base-package="com.xxx" />
</beans>
他的配置作用是使用spring的注解功能。
3)remoting-login.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- add by guolei -->
<bean name="/loginService"class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="loginService" />
<property name="serviceInterface"value="com.hxrainbow.crm.operator.service.LoginService" />
</bean>
</beans>
实际业务类是通过Spring的HessianServiceExporter类来暴露给客户端的。service:指定服务对应的业务类,也就是我们通过spring实例化出的service实现类。serviceInterface:指定业务类实现哪个接口。Spring推荐采用面向接口编程,因此,Hessian服务建议通过接口暴露。Hessian的远程服务名为/loginService。
4.配置hessia客户端
客户端去访问hessian,首先要有访问的service接口类,如果涉及到对象,还有有对象dto类,注意必须是序列化的(用正常人的思维去想,如果客户端没有service,你如何调用service中的方法)。如何使用发布出去的接口了,下面是例子:
HessianProxyFactory factory = new HessianProxyFactory();
String url = "http://192.168.2.188:8080/HXCRM/remoting/loginService";
loginService=(LoginService)factory.create(LoginService.class,
url);
在action的构造函数中加入上面的东西。我们就可以使用loginService了。
Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能.相比WebService,Hessian更简单、快捷。<
xmlnamespace prefix ="o" ns ="urn:schemas-microsoft-com:office:office" />
我们可以从hessian的定义中得到两个信息:
1) hessian主要功能是提供RMI功能。
2) hessian和webservice很像,他比较好学。
2.什么是RMI
RMI(Remote Method Invocation)是远程方法的简称,他能够帮我们查找并执行远程对象的方法。通俗的说,远程调用就象将一个class放在A机器上,然后在B机器中调用这个class的方法。
其他机器需要调用的对象必须被导出到远程注册服务器,这样才能被其他机器调用。因此,如果机器A要调用机器B上的方法,则机器B必须将该对象导出到其远程注册服务器。注册服务器是服务器上运行的一种服务,它帮助客户端远程地查找和访问服务器上的对象。一个对象只有导出来后,然后才能实现RMI包中的远程接口。例如,如果想使机器A中的Xyz对象能够被远程调用,它就必须实现远程接口。
3.配置hessian服务端
配置java服务端,需要满足以下条件:
1)包含hessian的jar包,spring jar包,spring-mvc
jar包
2)设计一个接口,用来给客户端调用
3)实现该接口的功能
4)配置web.xml(配置servlet)
5)对象必须实现Serializable接口
6)配置mvc配置文件和hessian配置文件
实现部分:
1)web.xml文件中
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.4"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<!-- 配置sruts2的中央控制器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<!-- 配置spring的监听器,加载Spring配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring/applicationContext-*.xml</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<!-- 开启监听 -->
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- hessian 定义spring的调试分配器 -->
<servlet>
<servlet-name>remoting</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring/remoting-servlet.xml,classpath:/spring/mvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>remoting</servlet-name>
<url-pattern>/remoting/*</url-pattern>
</servlet-mapping>
<!-- 自定义的监听类-->
<listener>
<listener-class>com.hxrainbow.crm.util.InitListener</listener-class>
</listener>
<!-- session超时定义,单位为分钟 ,默认为30分钟-->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<!-- 页面没找到 -->
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
<!-- 内部服务器错误 -->
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
</web-app>
配置文件中需要注意struts2过滤器部分的配置,因为我们发布的hessian接口没有后缀名,所以在访问的时候我们得做处理(修改配置文件),以免误以为访问struts2。还有就是注意spring的配置要在hessian的配置之前,因为hessian要使用spring中实例化的东西。
2)mvc-config.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- 启动springmvc的注解映射功能 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="1" />
</bean>
<!-- Hessian 启动BeanNameMapping 映射功能 -->
<bean
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="defaultHandler" ref="httpRequestHandlerAdapter" />
<property name="order" value="2" />
</bean>
<!-- 启动springmvc的注解功能 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!-- 处理httpRequest的Adapter
-->
<bean id="httpRequestHandlerAdapter"
class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean>
<!-- spring 对 com 进行扫描 -->
<context:component-scan base-package="com.xxx" />
</beans>
他的配置作用是使用spring的注解功能。
3)remoting-login.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- add by guolei -->
<bean name="/loginService"class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="loginService" />
<property name="serviceInterface"value="com.hxrainbow.crm.operator.service.LoginService" />
</bean>
</beans>
实际业务类是通过Spring的HessianServiceExporter类来暴露给客户端的。service:指定服务对应的业务类,也就是我们通过spring实例化出的service实现类。serviceInterface:指定业务类实现哪个接口。Spring推荐采用面向接口编程,因此,Hessian服务建议通过接口暴露。Hessian的远程服务名为/loginService。
4.配置hessia客户端
客户端去访问hessian,首先要有访问的service接口类,如果涉及到对象,还有有对象dto类,注意必须是序列化的(用正常人的思维去想,如果客户端没有service,你如何调用service中的方法)。如何使用发布出去的接口了,下面是例子:
HessianProxyFactory factory = new HessianProxyFactory();
String url = "http://192.168.2.188:8080/HXCRM/remoting/loginService";
loginService=(LoginService)factory.create(LoginService.class,
url);
在action的构造函数中加入上面的东西。我们就可以使用loginService了。
相关文章推荐
- SSH下使用Spring注解自动注入bean
- SSH下使用Spring注解自动注入bean
- Spring 注解Autowired自动注入bean异常解决
- Spring注解自动注入Bean
- spring 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入
- 使用Spring注解,在静态方法中注入bean
- servlet 中使用Spring自动注入的bean
- Spring整理系列(10)——@Autowired自动装配、结合@Qualifier过滤及与JSR-250@Resource注解、JSR-330@Inject注解的区别、@Named使用
- Spring在工具类中使用注解注入Bean
- 将spring管理的bean使用注解的方式注入到servlet中
- Spring注解自动注入Bean
- 7.5.6: Spring容器中的Bean---使用自动装配注入合作者Bean
- 类注解Spring注解自动注入Bean
- 框架 day36 Spring3 入门,DI依赖注入,装配bean基于xml/注解, 整合Junit4,配置约束自动提示
- hessian 在spring中的使用 (bean 如 Dao无法注入的问题)
- spring-使用注解实现Bean自动装配1
- Spring注解自动注入Bean
- action控制层,使用spring注解方式注入业务bean报空指向错误
- spring注解自动注入bean
- spring 在静态工具类中使用注解注入bean