出家如初,成佛有余

BlazeDS 与Spring集成指南

Posted in Uncategorized by chuanliang on 2009/07/21

    Springsource与Adobe合作发布了Spring与BlazeDS集成的项目Spring BlazeDS Integration,通过Spring BlazeDS Integration可以把Spring的Bean暴露为BlazeDS的Flex Remoting Service,这样Flex客户端就能够通过AMF调用Spring 暴露出来的Bean,有效简化BlazeDS配置及开发难度。

1、配置好BlazeDS的开发环境

参考“使用BlazeDS实现Java和Flex通信之hello world ”中的环境搭建过程,配置BlazeDS的开发环境。

创建com.yeeach.HelloWorldService,内容如下:

package com.yeeach;

public class HelloWorldService {
    public String hello(String var1) {
        return "hello " + var1;
    }
    public String world(String var1) {
        return "world " + var1;
    }
}

2、下载Spring BlazeDS Integration 1.0、Spring、jackson包

http://s3.amazonaws.com/dist.springframework.org/release/FLEX/spring-flex-1.0.0.RELEASE-with-dependencies.zip

http://s3.amazonaws.com/dist.springframework.org/release/SPR/spring-framework-2.5.6.SEC01-with-dependencies.zip

http://jackson.codehaus.org/0.9.4/jackson-asl-0.9.4.jar

放入到flex-spring/WebRoot/WEB-INF/lib

3、修改web.xml,内容如下:

<?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>Spring BlazeDS Integration Samples</display-name>
    <description>Spring BlazeDS Integration Sample Application</description>

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

    <!– The front controller of this Spring Web application, responsible for handling all application requests –>
    <servlet>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                   classpath:/applicationContext*.xml
                classpath*:/applicationContext.xml
                /WEB-INF/applicationContext*.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!– Map all *.spring requests to the DispatcherServlet for handling –>
    <servlet-mapping>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <url-pattern>/spring/*</url-pattern>
    </servlet-mapping>

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

</web-app>

4、修改flex/services-config.xml,内容如下

<?xml version="1.0" encoding="UTF-8"?>
<services-config>

    <services>
        <service-include file-path="remoting-config.xml" />
        <service-include file-path="proxy-config.xml" />

        <!–
        Application level default channels. Application level default channels are
        necessary when a dynamic destination is being used by a service component
        and no ChannelSet has been defined for the service component. In that case,
        application level default channels will be used to contact the destination.
        –>  
        <default-channels>
           <channel ref="my-amf"/>
        </default-channels>
    </services>

    <channels>
        <channel-definition id="my-streaming-amf" class="mx.messaging.channels.StreamingAMFChannel">
            <endpoint url="http://{server.name}:{server.port}/{context.root}/spring/messagebroker/streamingamf" class="flex.messaging.endpoints.StreamingAMFEndpoint"/>
        </channel-definition>
        <channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
            <endpoint url="http://{server.name}:{server.port}/{context.root}/spring/messagebroker/amf" class="flex.messaging.endpoints.AMFEndpoint"/>
            <properties>
                <polling-enabled>false</polling-enabled>
            </properties>
        </channel-definition>

        <channel-definition id="my-secure-amf" class="mx.messaging.channels.SecureAMFChannel">
            <endpoint url="https://{server.name}:{server.port}/{context.root}/spring/messagebroker/amfsecure" class="flex.messaging.endpoints.SecureAMFEndpoint"/>
            <properties>
                <add-no-cache-headers>false</add-no-cache-headers>
            </properties>
        </channel-definition>

        <channel-definition id="my-polling-amf" class="mx.messaging.channels.AMFChannel">
            <endpoint url="http://{server.name}:{server.port}/{context.root}/spring/messagebroker/amfpolling" class="flex.messaging.endpoints.AMFEndpoint"/>
            <properties>
                <polling-enabled>true</polling-enabled>
                <polling-interval-seconds>4</polling-interval-seconds>
            </properties>
        </channel-definition>

        <channel-definition id="my-http" class="mx.messaging.channels.HTTPChannel">
            <endpoint url="http://{server.name}:{server.port}/{context.root}/spring/messagebroker/http" class="flex.messaging.endpoints.HTTPEndpoint"/>
        </channel-definition>

        <channel-definition id="my-secure-http" class="mx.messaging.channels.SecureHTTPChannel">
            <endpoint url="https://{server.name}:{server.port}/{context.root}/spring/messagebroker/httpsecure" class="flex.messaging.endpoints.SecureHTTPEndpoint"/>
            <properties>
                <add-no-cache-headers>false</add-no-cache-headers>
            </properties>
        </channel-definition>

    </channels>

    <logging>
        <!– You may also use flex.messaging.log.ServletLogTarget –>
        <target class="flex.messaging.log.ConsoleTarget" level="info">
            <properties>
                <prefix>[BlazeDS] </prefix>
                <includeDate>false</includeDate>
                <includeTime>false</includeTime>
                <includeLevel>true</includeLevel>
                <includeCategory>false</includeCategory>
            </properties>
            <filters>
                <pattern>Endpoint.*</pattern>
                <pattern>Service.*</pattern>
                <pattern>Configuration</pattern>
            </filters>
        </target>
    </logging>

</services-config>

5、修改flex/remoting-config.xml,内容如下

<?xml version="1.0" encoding="UTF-8"?>
<service id="remoting-service"
    class="flex.messaging.services.RemotingService">

    <adapters>
        <adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/>
    </adapters>

    <default-channels>
        <channel ref="my-amf"/>
    </default-channels>
  </service>

6、修改applicationContext.xml,增加BlazeDS与Spring集成配置,将Spring的bean导出为flex的Destination

共有三种方法可以将Spring的bean导出为felx的Destination

创建WEB-INF/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.0.xsd">

    <!– Maps request paths at /messagebroker to the BlazeDS MessageBroker –>
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <value>
                /messagebroker/*=_messageBroker
            </value>
        </property>
    </bean>

    <!– Dispatches requests mapped to a MessageBroker –>
    <bean class="org.springframework.flex.servlet.MessageBrokerHandlerAdapter"/>

    <!– Bootstraps and exposes the BlazeDS MessageBroker –>
    <bean id="_messageBroker " class="org.springframework.flex.core.MessageBrokerFactoryBean" />   

    <!– Expose the productService bean for BlazeDS remoting –>
    <!– method 1 –>
    <!–
      <bean id="test1" class="com.yeeach.HelloWorldService" />
      <flex:remoting-destination ref="test1" />
      –>
      <!–  method2 –>
      <!–
      <bean id="test1" class="com.yeeach.HelloWorldService" >
          <flex:remoting-destination />
      </bean>
       –>
      <!–  method3 –>
      <bean id="helloWorldService" class="com.yeeach.HelloWorldService" />
      <bean id="test1" class="org.springframework.flex.remoting.RemotingDestinationExporter">
          <property name="messageBroker" ref="mySpringManagedMessageBroker"/>
        <property name="service" ref="helloWorldService"/>
      </bean>
</beans>

7、创建flex-src/helloworld.mxml,内容如下

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                layout="vertical">
    <mx:RemoteObject destination="test1"
                     id="test1">
        <mx:method name="hello"
                   result="sayHelloResult(event)"/>
        <mx:method name="world"
                   result="sayWorldResult(event)"/>
    </mx:RemoteObject>
    <mx:HBox>
        <mx:Label text="输入:"/>
        <mx:TextInput id="inputStr"/>
        <mx:Button label="say hello"
                   click="sayHello(event);"/>
        <mx:Button label="say world"
                   click="sayWorld(event);"/>
    </mx:HBox>
    <mx:HBox>
        <mx:Label text="结果:"/>
        <mx:TextArea id="result"/>
    </mx:HBox>

    <mx:Script>

        <![CDATA[
            import mx.rpc.events.FaultEvent;
            import mx.controls.Alert;
            import mx.rpc.events.ResultEvent;

            function sayHello(event:Event):void
            {
                var inputVar:String=inputStr.text;
                test1.hello(inputVar);

            }

            function sayWorld(event:Event):void
            {
                var inputVar:String=inputStr.text;
                test1.world(inputVar);

            }

            private function sayHelloResult(event:ResultEvent):void
            {
                result.text=event.result.toString();
                Alert.show(event.result.toString(), "返回结果");
            }

            private function sayWorldResult(event:ResultEvent):void
            {
                result.text=event.result.toString();
                Alert.show(event.result.toString(), "返回结果");
            }
        ]]>
    </mx:Script>
</mx:Application>

8、参考文档:

    http://static.springsource.org/spring-flex/docs/1.0.x/reference/html/index.html

    http://blog.springsource.com/2009/06/10/using-spring-blazeds-integration-10/

    http://blog.springsource.com/2008/12/17/using-spring-blazeds-integration-m1/

    http://ria.dzone.com/articles/introduction-spring-blazeds?page=0,2

 

程序代码:flex-spring.rar

 

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: