`
limingnihao
  • 浏览: 1820536 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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/*。改完效果如下实例:

<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项目;

 

 

5
0
分享到:
评论
4 楼 chao_1990 2015-12-21  
jar包都下载不了, ,求一个war包,2225290081@qq.com
3 楼 crab530143383 2015-01-13  
  
2 楼 zyflaou 2012-03-27  
很好,谢谢
1 楼 kaidi0314 2011-11-16  
不错,顶,谢谢

相关推荐

Global site tag (gtag.js) - Google Analytics