二、配置使用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/*。改完效果如下實例:
<servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/messagebroker/*</url-pattern> </servlet-mapping>
2.1.3在Spring中配置MessageBroker
Spring提供了簡化的XML配置命令來再在Spring的WebApplicationContext中配置(dispatcher-servlet.xml)MessageBroker,需要是這些命名空間支持需要在SpringXML配置文件中添加相應的架構。
典型配置如下例子:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:flex="http://www.springframework.org/schema/flex" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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/flex http://www.springframework.org/schema/flex/spring-flex-1.0.xsd"> </beans>
這使Spring BlazeDS 集成了flex命名空間,可在配置文件中使用。
最基本的配置,需要在Spring的WebApplicationContext中聲明一個MessageBrokerFactoryBean的Bean,以便將傳去的請求引導到MessageBroder中,以及Spring管理MessageBroker需要的MessageBrokerHandlerAdapter和HandlerMapping(一般爲SimpleUrlHandlerMapping)。
這些bean可以使用message-broker標籤來自動註冊。例如最簡單的方法:
<flex:message-broker/>
這樣設置MessageBroker和必要的基礎默認配置。默認值可以被重寫,配置message-broker標籤的屬性和他的子元素。
例如BlazeDS XML配置文件默認位置爲:/ WEB-INF/flex/services-config.xml。但可以使用services-config-path屬性重新配置路徑。classpath在maven項目中默認爲src/main/resources路徑下。
<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。如下示例:
<bean id="flexGeneralController" class="com.springFlex.example.view.flex.FlexGeneralController"/> <flex:remoting-destination ref="flexGeneralController"/>
也可以寫成:
<bean id="flexGeneralController" class="com.springFlex.example.view.flex.FlexGeneralController"> <flex:remoting-destination/> </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:
@Controller(value="flexGeneralController")
@RemotingDestination(value="flexGeneralController", channels="my-amf")
public class FlexGeneralController {
@RemotingInclude
public String getName(String name) {
return "hello:" + name;
}
@RemotingExclude
public double getSqrt(int number) {
return Math.sqrt(number);
}
}
@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。
如下例子:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
minWidth="955" minHeight="600">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.messaging.ChannelSet;
import mx.messaging.channels.AMFChannel;
import mx.rpc.Fault;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
protected function button1_clickHandler(event:MouseEvent):void
{
this.remoteObject.getName("limingnihao");
}
protected function button2_clickHandler(event:MouseEvent):void
{
this.remoteObject.getSqrt(5);
}
protected function getStringResultHandler(event:ResultEvent):void{
Alert.show(event.result.toString());
}
protected function getSqrtResultHandler(event:ResultEvent):void{
Alert.show(event.result.toString());
}
protected function remoteobject1_faultHandler(event:FaultEvent):void
{
Alert.show(event.fault.toString());
}
]]>
</mx:Script>
<mx:RemoteObject id="remoteObject" destination="flexGeneralController" fault="remoteobject1_faultHandler(event)"
endpoint="http://192.168.1.119:8080/SpringFlexExample_JavaService/messagebroker/amf">
<mx:method name="getName" result="getStringResultHandler(event)"/>
<mx:method name="getSqrt" result="getSqrtResultHandler(event)"/>
</mx:RemoteObject>
<mx:Button x="37" y="64" label="按鈕1" click="button1_clickHandler(event)"/>
<mx:Button x="130" y="64" label="按鈕2" click="button2_clickHandler(event)"/>
</mx:Application>
2.3消息攔截器MessageInterceptor
自定義消息攔截器,可以用於處理特殊邏輯在AMF傳入傳出java 表單的時候。例如,用攔截器檢查傳入消息的內容,或者給返回信息進行額外統一的操作。
通過實現org.springframework.flex.core.MessageInterceptor接口就可以進行自定義message攔截器。然後必須配置成Spring Bean,在用XML命名空間關聯此bean。
如下實例:自定義message攔截器:CustomMessageInterceptor,和xml文件的配置方法。
package com.springFlex.example.exception;
import org.springframework.flex.core.MessageInterceptor;
import org.springframework.flex.core.MessageProcessingContext;
import flex.messaging.messages.Message;
public class CustomMessageInterceptor implements MessageInterceptor {
public Message postProcess(MessageProcessingContext context, Message inputMessage, Message outputMessage) {
System.out.println("CustomMessageInterceptor - postProcess");
return outputMessage;
}
public Message preProcess(MessageProcessingContext context, Message inputMessage) {
System.out.println("CustomMessageInterceptor - preProcess");
return inputMessage;
}
}
需要在xml文件中添加bean,和關聯bean。
<bean id="customMessageInterceptor" class="com.springFlex.example.exception.CustomMessageInterceptor"/> <flex:message-broker> <flex:message-interceptor ref="customMessageInterceptor"/> </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文件的配置方法。
package com.springFlex.example.exception;
import org.springframework.flex.core.ExceptionTranslator;
import flex.messaging.MessageException;
public class CustomExceptionTranslator implements ExceptionTranslator {
public boolean handles(Class<?> clazz) {
System.out.println("CustomExceptionTranslator - handles - " + clazz.getName());
return true;
}
public MessageException translate(Throwable t) {
System.out.println("CustomExceptionTranslator - translate - " + t.getMessage());
return new MessageException(t);
}
}
需要在xml文件中添加bean,和關聯bean。
<bean id="customExceptionTranslator" class="com.springFlex.example.exception.CustomExceptionTranslator"/> <flex:message-broker> <flex:exception-translator ref="customExceptionTranslator"/> </flex:message-broker>
附件中有源代碼:
SpringFlexExample_JavaService爲Java項目;
SpringFlexExample_FlexClient.rar爲Flex項目;