BlazeDs + Spring 整合

在Flex + BlazeDS + java 整合完成的基础上

1. 修改web.xml配置文件。原来是通过blazeds拦截,现在改成用spring 拦截。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>

    <display-name>BlazeDS</display-name>
    <description>BlazeDS Application</description>

    <!-- Http Flex Session attribute and binding listener support -->
    <listener>
        <listener-class>flex.messaging.HttpFlexSession</listener-class>
    </listener>

     <!-- 定义 spring flex 集成的配置,使用单独的配置文件。
        不使用在一个applicatonContext.xml中import springFlex.xml的方法 
        spring flex 一定要用DispatcherServlet 方法。不能用ContextLoaderListener -->
    <servlet>
        <servlet-name>MessagebrokerServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <!-- Map /spring/* requests to the DispatcherServlet -->
    <servlet-mapping>
        <servlet-name>MessagebrokerServlet</servlet-name>
        <url-pattern>/messagebroker/*</url-pattern>
    </servlet-mapping>
    <!-- 定义 spring flex 集成的配置  结束-->

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
    </welcome-file-list>

    <!-- for WebSphere deployment, please uncomment -->
    <!--
    <resource-ref>
        <description>Flex Messaging WorkManager</description>
        <res-ref-name>wm/MessagingWorkManager</res-ref-name>
        <res-type>com.ibm.websphere.asynchbeans.WorkManager</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
    -->

</web-app>

2. 添加Spring配置文件:applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:flex="http://www.springframework.org/schema/flex" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
		http://www.springframework.org/schema/flex 
		http://www.springframework.org/schema/flex/spring-flex-1.5.xsd">

	<!-- spring 配置文件来定义 -->
	<flex:message-broker>
		<flex:message-service default-channels="my-amf, my-polling-amf" />
	</flex:message-broker>
	<bean id="hello" class="test.HelloWord">
		<flex:remoting-destination/>
	</bean>
</beans>

3.Flex客户端调用

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   xmlns:mx="library://ns.adobe.com/flex/mx"
			   creationComplete="application1_creationCompleteHandler(event)">
	
	<fx:Script>
		<![CDATA[
			import mx.controls.Alert;
			import mx.events.FlexEvent;
			import mx.rpc.events.FaultEvent;
			import mx.rpc.events.ResultEvent;
			
			
			protected function application1_creationCompleteHandler(event:FlexEvent):void
			{
				ro.getName("XOXOXO");
			}
			
			protected function ro_resultHandler(event:ResultEvent):void
			{
				Alert.show(event.result as String);
				
			}
			
			protected function ro_faultHandler(event:FaultEvent):void
			{
				Alert.show(event.fault.faultString);	
			}
			
		]]>
	</fx:Script>
	
	<fx:Declarations>
		<s:RemoteObject id="ro"
						result="ro_resultHandler(event)"
						fault="ro_faultHandler(event)"
						destination="hello"/>
	</fx:Declarations>
</s:Application>

* 启动服务器,开始运行,弹出 发送失败。原因是因为没有定义endpoint的,但是有不希望在Flex客户端中硬编码写入endpoint。怎么解决呢?

1. 先在Flex项目中添加一个编译参数:-services "D:\carsonsoft\commhome\webapps\DLUvsServer\WEB-INF\flex\services-config.xml"

2. 在services-config.xml中添加默认的通道和端点:

<services>
	<service-include file-path="remoting-config.xml" />
	<service-include file-path="proxy-config.xml" />
	<service-include file-path="messaging-config.xml" />
	<default-channels>
		<channel ref="my-amf" />
	</default-channels>
</services>


Spring + BlazeDS 整合完毕。很简单吧。不过我整合的时候发了将近一天。走了不少弯路呀。

你可能感兴趣的:(spring,Flex,blazeds,Blazeds+Spring)