SpringFlex框架搭建 之 二、配置使用SpringFlex

二、配置使用SpringFlex

2.1SpringFlex的基本配置

        Spring与BlazeDS结合必须使用MessageBroker,Flex客户端的HTTP请求将有Spring的DispatcherServlet分发到Spring管理的MessageBroker。使用Spring管理MessageBroker就无需配置BlazeDS MessageBrokerServlet了。

 

 

2.1.1添加Flex配置文件和BlazeDS包

        添加BlazeDS相关的jar包:
通过pom搜索blazeds添加相应的jar包。可能版本不是最新的。
或者自行在blazeds-turnkey-4.0.0.rar解压的文件中(路径: blazeds-turnkey-4.0.0.14931\tomcat\webapps\blazeds\WEB-INF\lib)。

        添加XML配置文件:
将下载的BlazeDS解压,如:在blazeds-turnkey-4.0.0.14931\tomcat\webapps\blazeds\WEB-INF路径下的flex整个文件,copy到项目中WEB-INF下。

 

 

 

2.1.2配置Spring的DispatcherServlet

将Spring的DispatcherServlet对应的servlet-mapping的url-pattern改成/messagebroker/*。改完效果如下实例:

Xml代码   收藏代码
  1. <servlet>  
  2.     <servlet-name>dispatcher</servlet-name>  
  3.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  4.     <init-param>  
  5.         <param-name>contextConfigLocation</param-name>  
  6.         <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>  
  7.     </init-param>  
  8.     <load-on-startup>1</load-on-startup>  
  9. </servlet>  
  10. <servlet-mapping>  
  11.     <servlet-name>dispatcher</servlet-name>  
  12.     <url-pattern>/messagebroker/*</url-pattern>  
  13. </servlet-mapping>  
  14.    

 

 

 

2.1.3在Spring中配置MessageBroker

Spring提供了简化的XML配置命令来再在Spring的WebApplicationContext中配置(dispatcher-servlet.xml)MessageBroker,需要是这些命名空间支持需要在SpringXML配置文件中添加相应的架构。

典型配置如下例子:

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xmlns:flex="http://www.springframework.org/schema/flex" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/aop   
  7.         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
  8.         http://www.springframework.org/schema/beans   
  9.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
  10.         http://www.springframework.org/schema/context   
  11.         http://www.springframework.org/schema/context/spring-context-3.0.xsd   
  12.         http://www.springframework.org/schema/mvc   
  13.         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd   
  14.         http://www.springframework.org/schema/tx   
  15.         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  16.         http://www.springframework.org/schema/flex   
  17.         http://www.springframework.org/schema/flex/spring-flex-1.0.xsd">  
  18.   
  19. </beans>  

 

 

        这使Spring BlazeDS 集成了flex命名空间,可在配置文件中使用。
        最基本的配置,需要在Spring的WebApplicationContext中声明一个MessageBrokerFactoryBean的Bean,以便将传去的请求引导到MessageBroder中,以及Spring管理MessageBroker需要的MessageBrokerHandlerAdapter和HandlerMapping(一般为SimpleUrlHandlerMapping)。
        这些bean可以使用message-broker标签来自动注册。例如最简单的方法:

Xml代码   收藏代码
  1. <flex:message-broker/>  

 

        这样设置MessageBroker和必要的基础默认配置。默认值可以被重写,配置message-broker标签的属性和他的子元素。
        例如BlazeDS XML配置文件默认位置为:/ WEB-INF/flex/services-config.xml。但可以使用services-config-path属性重新配置路径。classpath在maven项目中默认为src/main/resources路径下。

Xml代码   收藏代码
  1. <flex:message-broker services-config-path="classpath*:services-config.xml"/>  

 

 

2.2Flex Remoting调用Spring Bean

使用Spring管理MessageBroker可以使Spring的beans很方便的被Flex客户端直接远程调用。MessageBroker处理在Flex AMF与Java传送的序列化和反序列化的数据格式。

 

2.2.1配置Remoting Service

通过xml配置文件进行配饰Remoting Service时,只需要声明这个允许被调用的Java类的一个bean,再将这个bean声明成一个remoting-destination。如下示例:

Xml代码   收藏代码
  1. <bean id="flexGeneralController" class="com.springFlex.example.view.flex.FlexGeneralController"/>  
  2. <flex:remoting-destination ref="flexGeneralController"/>  

 

也可以写成:

Xml代码   收藏代码
  1. <bean id="flexGeneralController" class="com.springFlex.example.view.flex.FlexGeneralController">  
  2.     <flex:remoting-destination/>  
  3. </bean>  

 
 使用上面方式声明remoting-destination时,必须保证添加了flex:message-broker标签。

 

 

 

2.2.2使用@RemotingDestination

        @RemotingDestination注解用于基于注解的remoting-destination配置而替换xml方法。@RemotingDestination使用在类级别上一标记此类为remoting-destination。@ RemotingInclude和 @ RemotingExclude注解用在方法级别上,进行标记@RemotingDestination注解类中的方法,是“包括”还是“不包括”此类remoting-destination远程调用。
        另,被@RemotingDestination注解的类,必须声明成一个bean。例如Service层或Controller层的功能类。

 

        下面给出一个简单的例子,controller层类被声明成remoting-destination:

Java代码   收藏代码
  1. @Controller(value="flexGeneralController")  
  2. @RemotingDestination(value="flexGeneralController", channels="my-amf")  
  3. public class FlexGeneralController {  
  4.   
  5.     @RemotingInclude  
  6.     public String getName(String name) {  
  7.         return "hello:" + name;  
  8.     }  
  9.       
  10.     @RemotingExclude  
  11.     public double getSqrt(int number) {  
  12.         return Math.sqrt(number);  
  13.     }  
  14. }  

 

 @RemotingDestination中的value为destination的id(Flex端的RemoteObject需要配置此属性),channels为AMP通道。

 

 

 

2.2.3Flex端的RemoteObject

在Flex端,使用RemoteObject就可以直接调用Java端的remoting-destination中的方法。需要配置endpoint和destination。
Endpoint一般路径为:http://[java项目IP地址]:[端口号]/[项目名称]/messagebroker/amf;
Destination为Java端的remoting-destination的id。例如在2.2.2中的例子中,Destination值为flexGeneralController。


 如下例子:

Java代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"   
  3.                 minWidth="955" minHeight="600">  
  4.     <mx:Script>  
  5.         <![CDATA[  
  6.             import mx.controls.Alert;  
  7.             import mx.messaging.ChannelSet;  
  8.             import mx.messaging.channels.AMFChannel;  
  9.             import mx.rpc.Fault;  
  10.             import mx.rpc.events.FaultEvent;  
  11.             import mx.rpc.events.ResultEvent;  
  12.               
  13.             protected function button1_clickHandler(event:MouseEvent):void  
  14.             {  
  15.                 this.remoteObject.getName("limingnihao");  
  16.             }  
  17.               
  18.             protected function button2_clickHandler(event:MouseEvent):void  
  19.             {  
  20.                 this.remoteObject.getSqrt(5);  
  21.             }  
  22.               
  23.             protected function getStringResultHandler(event:ResultEvent):void{  
  24.                 Alert.show(event.result.toString());  
  25.             }  
  26.               
  27.             protected function getSqrtResultHandler(event:ResultEvent):void{  
  28.                 Alert.show(event.result.toString());  
  29.             }  
  30.             protected function remoteobject1_faultHandler(event:FaultEvent):void  
  31.             {  
  32.                 Alert.show(event.fault.toString());  
  33.             }  
  34.               
  35.         ]]>  
  36.     </mx:Script>  
  37.       
  38.     <mx:RemoteObject id="remoteObject" destination="flexGeneralController" fault="remoteobject1_faultHandler(event)"   
  39.                      endpoint="http://192.168.1.119:8080/SpringFlexExample_JavaService/messagebroker/amf">  
  40.         <mx:method name="getName" result="getStringResultHandler(event)"/>  
  41.         <mx:method name="getSqrt" result="getSqrtResultHandler(event)"/>  
  42.     </mx:RemoteObject>  
  43.       
  44.     <mx:Button x="37" y="64" label="按钮1" click="button1_clickHandler(event)"/>  
  45.     <mx:Button x="130" y="64" label="按钮2" click="button2_clickHandler(event)"/>  
  46.       
  47. </mx:Application>  

 


 

 

2.3消息拦截器MessageInterceptor


自定义消息拦截器,可以用于处理特殊逻辑在AMF传入传出java 表单的时候。例如,用拦截器检查传入消息的内容,或者给返回信息进行额外统一的操作。
通过实现org.springframework.flex.core.MessageInterceptor接口就可以进行自定义message拦截器。然后必须配置成Spring Bean,在用XML命名空间关联此bean。

 

如下实例:自定义message拦截器:CustomMessageInterceptor,和xml文件的配置方法。

Java代码   收藏代码
  1. package com.springFlex.example.exception;  
  2.   
  3. import org.springframework.flex.core.MessageInterceptor;  
  4. import org.springframework.flex.core.MessageProcessingContext;  
  5.   
  6. import flex.messaging.messages.Message;  
  7.   
  8. public class CustomMessageInterceptor implements MessageInterceptor {  
  9.   
  10.     public Message postProcess(MessageProcessingContext context, Message inputMessage, Message outputMessage) {  
  11.         System.out.println("CustomMessageInterceptor - postProcess");  
  12.         return outputMessage;  
  13.     }  
  14.   
  15.     public Message preProcess(MessageProcessingContext context, Message inputMessage) {  
  16.         System.out.println("CustomMessageInterceptor - preProcess");  
  17.         return inputMessage;  
  18.     }  
  19.   
  20. }  

 

需要在xml文件中添加bean,和关联bean。

Xml代码   收藏代码
  1. <bean id="customMessageInterceptor" class="com.springFlex.example.exception.CustomMessageInterceptor"/>  
  2. <flex:message-broker>  
  3.     <flex:message-interceptor ref="customMessageInterceptor"/>  
  4. </flex:message-broker>  

 

 

 

 

2.4异常转换器Exception Translators

        在服务器发生异常时,会将这个异常对象传播到Flex客户端,但必须将这个原始异常转换翻译成flex.messaging.MessageException 的一个实例。如果不进行翻译工作,一般“Server.Processing”错误将传播到Flex客户端,这样客户端就无法根据错误的原因而作处理。通常情况下,将转换成Spring安全异常MessageException,但也可以使用自定义的应用程序级别异常进行转换。
        通过实现org.springframework.flex.core.ExceptionTranslator接口进行自定义异常转换器。然后必须配置成Spring Bean,在用XML命名空间关联此bean。
        接口的handles方法的返回值来控制是否进行翻译。返回false不进行翻译工作。返回true则就会去执行接口的translate方法进行异常转换。


        如下实例:自定义Exception Translators:CustomExceptionTranslator,和xml文件的配置方法。

Java代码   收藏代码
  1. package com.springFlex.example.exception;  
  2.   
  3. import org.springframework.flex.core.ExceptionTranslator;  
  4.   
  5. import flex.messaging.MessageException;  
  6.   
  7. public class CustomExceptionTranslator implements ExceptionTranslator {  
  8.   
  9.     public boolean handles(Class<?> clazz) {  
  10.         System.out.println("CustomExceptionTranslator - handles - " + clazz.getName());  
  11.         return true;  
  12.     }  
  13.   
  14.     public MessageException translate(Throwable t) {  
  15.         System.out.println("CustomExceptionTranslator - translate - " + t.getMessage());  
  16.         return new MessageException(t);  
  17.     }  
  18. }  

 

需要在xml文件中添加bean,和关联bean。

Xml代码   收藏代码
  1. <bean id="customExceptionTranslator" class="com.springFlex.example.exception.CustomExceptionTranslator"/>  
  2. <flex:message-broker>  
  3.     <flex:exception-translator ref="customExceptionTranslator"/>  
  4. </flex:message-broker>  

  

 

 附件中有源代码:

SpringFlexExample_JavaService为Java项目;

SpringFlexExample_FlexClient.rar为Flex项目;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值