`
duobin3000
  • 浏览: 102519 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

flex-spring如何集成

阅读更多
Using Flex with Spring
  ----on detail,please see :http://coenraets.org/flex-spring/
UPDATE (1/12/2007): I put together a Tomcat-based Test Drive Server that includes the samples described below running out-of-the box. Read this post for more info.

What is Spring?
Spring is one of the most popular Java frameworks. The foundation of the Spring framework is a lightweight component container that implements the Inversion of Control (IoC) pattern.

Using an IoC container, components don’t instantiate or even look up their dependencies (the objects they work with). The container is responsible for injecting those dependencies when it creates the components (hence the term “Dependency Injection” also used to describe this pattern).

The result is looser coupling between components. The Spring IoC container has proven to be a solid foundation for building robust enterprise applications. The components managed by the Spring IoC container are called Spring beans.

The Spring framework includes several other modules in addition to its core IoC container. These modules are not covered in this document even though we will be using the Spring JDBC abstraction framework in examples 2 and 3 below. More information on the Spring framework can be found at http://www.springframework.org.

What is Flex?
Flex provides a complete solution for building Rich Internet Applications. The Flex programming model is made of:

ActionScript, an ECMAScript compliant, object-oriented programming model. With some syntactical differences, ActionScript looks and feels similar to Java, and supports the same object-oriented constructs: packages, classes, inheritance, interfaces, strong (but also dynamic) typing etc.
MXML: an XML-based language that provides an abstraction on top of ActionScript, and allows parts of an application (typically the View) to be built declaratively.
An extensive set of class libraries. The online API documentation is available here in a Javadoc-like format.
The Flex source code (.mxml and .as files) is compiled into Flash bytecode (.swf) that is executed at the client-side by the Flash virtual machine using a Just-In-Time compiler.

A complete discussion of Flex is beyond the scope of this document. You can find more information at http://www.adobe.com/products/flex.

How does Flex access back-end systems?
When writing Flex applications, you can access back-end systems using four different strategies:

You can use the HTTPService component to send HTTP requests to a server, and consume the response. Although the HTTPService is typically used to consume XML, it can be used to consume other types of responses. The Flex HTTPService is similar to the XMLHttpRequest component available in Ajax.
You can use the WebService component to invoke SOAP-based web services.
You can use the RemoteObject component to directly invoke methods of Java objects deployed in your application server, and consume the return value. The return value can be a value of a primitive data type, an object, a collection of objects, an object graph, etc. In distributed computing terminology, this approach is generally referred to as “remoting”. This is also the terminology used in Spring to describe how different clients can access Spring beans remotely.
In addition to the RPC-type services described above, the Flex Data Management Services provide an innovative and virtually code-free approach to synchronize data between the client application and the middle-tier.
In this document, we focus on the Remoting (3) and Data Management Services (4) approaches described above because they enable the tightest integration with Spring. There is no need to transform data, or to expose services in a certain way: the Flex application works directly with the beans registered in the Spring IoC container.

How does Flex access Spring beans?
So, if Flex clients can remotely access Java objects, and if Spring beans are Java objects, aren’t we all set and ready to start accessing Spring beans from Flex clients? Almost… There is one simple element to configure.

The whole idea behind Spring IoC is to let the container instantiate components (and inject their dependencies). By default, however, components accessed remotely by a Flex client are instantiated by Flex destinations at the server-side. The key to the Flex/Spring integration is therefore to configure the Flex destinations to let the Spring container take care of instantiating Spring beans. The Flex Data Services support the concept of factory to enable this type of custom component instantiation. The role of a factory is simply to provide ready-to-use instances of components to a Flex destination (instead of letting the Flex destination instantiate these components itself).

The supporting files available with this document include a factory class (SpringFactory) that provides Flex destinations with fully initialized (dependency-injected) instances of Spring beans. Note: The SpringFactory was developed by Jeff Vroom (Flex Data Services architect) and is also available on Adobe Exchange.

The remaining of this article describes how to configure your web application to use Flex and Spring, how to configure the Spring Factory, and how to put the pieces together and start invoking Spring beans from Flex applications.

Setting Up your Web Application to Use Flex and Spring
Step 1: Install the supporting files
Download flex-spring.zip here
Expand flex-spring.zip
flex-spring.zip includes the Spring factory as well as the supporting files for the examples below.

Step 2: Install Flex Data Services
To use the Remoting and Data Management Services data access strategies described above, you need to install the Flex Data Services. If you haven’t already done so, you can download the Flex Data Services here, and follow the installation instructions.

The installation process will install three web applications (flex, samples, and flex-admin). You can use either the flex or samples web application to run the examples below.

You can read more information on the installation process here: http://www.adobe.com/support/documentation/en/flex/2/install.html#installingfds2

Step 3: Install Spring
Note: A complete discussion of the Spring installation process is beyond the scope of this article. Refer to http://www.springframework.org for more information. The steps below describe a basic configuration that is sufficient for the purpose of this article.

Download the Spring framework (version 2.0) at http://www.springframework.org/download (the version without dependencies is sufficient to complete the examples in this article).

Note: The examples below have been developed and tested using Spring 2.0. However the integration approach described in this document (and the SpringFactory class) should work fine using Spring 1.2.8 (some of the examples might not work because they use Spring 2.0 features).
Expand the downloaded file
Locate spring.jar in the dist directory and copy it in the {context-root}\WEB-INF\lib directory of your web application
Modify the web.xml file of your web application. Add the context-param and listener definitions as follows:
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Step 4: Register the Spring Factory
Copy SpringFactory.class and SpringFactory$SpringFactoryInstance.class from flex-spring-sdk\bin\flex\samples\factories to {context-root}\WEB-INF\classes\flex\samples\factories
Register the Spring factory in {context-root}\WEB-INF\flex\services-config.xml:
<factories>
     <factory id="spring" class="flex.samples.factories.SpringFactory"/>
</factories>Example 1: Mortgage Calculator Using Flex Remoting
This first application is intentionally simplistic in its functionality to provide an uncluttered example of wiring Spring beans together and invoking them from a Flex application.

Step 1: Copy the application files
Copy RateFinder.class, SimpleRateFinder.class, and Mortgage.class from flex-spring-sdk\bin\flex\samples\spring\mortgage to {context-root}\WEB-INF\classes\flex\samples\spring\mortgage
Copy mortgage.mxml from flex-spring-sdk\flex\mortgage to {context-root}\mortgage
Step 2: Register Spring Beans
Before registering the Spring beans for this application, open RateFinder.java, SimpleRateFinder.java and Mortgage.java in flex-spring-sdk\src\flex\samples\spring\mortgage to familiarize yourself with the source code. Notice that Mortgage has a dependency to a RateFinder object. Mortgage doesn’t instantiate a RateFinder object itself, doesn’t lookup up for a RateFinder object, and doesn’t even know the exact type of the object it will be dealing with (RateFinder is an inteface). An instance of a class implementing the RateFinder interface will be injected by the container (using the setRateFinder method) when it instantiates the component.
If it doesn’t already exist, create a file named applicationContext.xml in {context-root}\WEB-INF.
Register the rateFinderBean and mortgageBean beans in applicationContext.xml as follows: 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

    <bean id="rateFinderBean" class="flex.samples.spring.mortgage.SimpleRateFinder"/>

    <bean id="mortgageBean" class="flex.samples.spring.mortgage.Mortgage">
          <property name="rateFinder" ref="rateFinderBean"/>
    </bean>

</beans>Notice that in the mortgageBean definition, we tell the container how to inject the rateFinder dependency: the rateFinder property is mapped to rateFinderBean which defines an instance of the SimpleRateFinder class.

Step 3: Configure the Flex Remoting Destination
Open remoting-config.xml in {context-root}\WEB-INF\flex.
Add a mortgageService destination as follows:   
<destination id="mortgageService">
    <properties>
        <factory>spring</factory>
        <source>mortgageBean</source>
    </properties>
</destination>Notice that we use the spring factory defined above (see “Register the Spring Factory”), and we provide the name of the Spring bean as defined in applicationContext.xml as the source.

Step 4: Run the Client Application
Open {context-root}\mortgage\MortgageCalc.mxml in a code editor to familiarize yourself with the application. Notice that the RemoteObject destination is the mortgageService destination defined above.
Open a browser, access http://host:port/context-root/mortgage/MortgageCalc.mxml, and test the application: Enter a loan amount and click “Calculate” to get the monthly payment for a 30 year mortgage.
Note: that there is a delay the first time you access an application in this manner. This is because we are using the web compiler which compiles your application into bytecode the first time it is accessed (similar to the JSP compilation model). Subsequent requests to the same application will be much faster since the application is already compiled. In a production environment, you would typically deploy applications that have already been compiled using the Flex compiler available as a command-line utility or fully integrated in FlexBuilder (the Eclipse-based development environment for Flex).
Depending on your configuration, you may need to increase the heap size of your application server’s JVM to use the web compiler. This would not be required in a production environment since you typically don’t use the web compiler. If you get a java.lang.OutOfMemoryError exception while trying to access a sample for the first time, you must increase your heap size. Alternatively, you can compile the application using FlexBuilder or the command line compiler.

Example 2: Store/Inventory Management using Flex Remoting
This second example is more sophisticated and includes database connectivity. To keep the application simple and avoid dependencies on other products or frameworks, the Spring JDBC abstraction framework is used to access the database. You could use the Spring support for ORM data access (using Hibernate, JDO, Oracle TopLink, iBATIS, or JPA) as an alternative: the specific Spring data access strategy you choose has no impact on the Flex/Spring integration. We use an embedded HSQLDB database: the only installation requirement is to copy the HSQLDB driver in your web application classpath (see below). The application has two modules: a database maintenance module, and a customer-facing product catalog with filtering capabilities.

Step 1: Copy the application files
Copy hsqldb.jar to {context-root}\WEB-INF\lib. You can download the hsqldb driver at http://www.hsqldb.org/.
Copy ProductDAO.class, SimpleProductDAO.class, SimpleProductDAO$1.class, and Product.class from flex-spring-sdk\bin\flex\samples\spring\store to {context-root}\WEB-INF\classes\flex\samples\spring\store
Copy admin.mxml, ProductForm.mxml, store.mxml, Thumb.mxml, AnimatedTileList.as, Product.as, logo.jpg, store.css and the pic directory from flex-spring-sdk\flex\store to {context-root}\store
Step 2: Register Spring Beans
Before registering the Spring beans for this application, open ProductDAO.java, SimpleProductDAO.java and Product.java in flex-spring-sdk\src\flex\samples\spring\store to familiarize yourself with the source code. Notice that SimpleProductDAO extends org.springframework.jdbc.core.support.JdbcDaoSupport. JdbcDaoSupport has a dependency to a javax.sql.DataSource object (javax.sql.DataSource is an interface).
Register the dataSource and productDAOBean beans in applicationContext.xml as follows: 
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
    <property name="url" value="jdbc:hsqldb:/flex-spring-sdk/db/store"/>
    <property name="username" value="sa"/>
    <property name="password" value=""/>
</bean>

<bean id="productDAOBean" class="flex.samples.spring.store.SimpleProductDAO">
    <property name="dataSource" ref="dataSource"/>
</bean> Note: If you didn’t unzip flex-spring-sdk in your root directory, adjust the JDBC url accordingly.

Step 3: Configure the Flex Remoting Destination
Open remoting-config.xml in {context-root}\WEB-INF\flex.
Add the productService destination as follows:  
<destination id="productService">
    <properties>
        <factory>spring</factory>
        <source>productDAOBean</source>
    </properties>
</destination>Step 4: Run the Client Application
Test the database maintenance module:

Open {context-root}\store\admin.mxml in a code editor and familiarize yourself with the application.
Open a browser, access http://host:port/context-root/store/admin.mxml, and test the application.
Test the store module:

Open {context-root}\store\store.mxml in a code editor and familiarize yourself with the application.
Open a browser, access http://host:port/context-root/store/store.mxml, and test the application.
Example 3: Data Management Services
In addition to the RPC services used in examples 1 and 2 above, the Flex Data Management Services provide an innovative and very productive approach to synchronize data between the client and the middle-tier. The Flex Data Management Services consist of a client-side API and server-side services:

At the client-side, "managed objects" keep track of changes made to the data, and notify the back-end of these changes. You don’t have to keep track of changes made to the data, nor do you have to invoke remote services to notify the back-end of the changes (create, update, delete) made at the client side.
At the server-side, the Data Service receives the list of changes and passes it to your server-side persistence components. The Data Service also pushes the changes to other clients.
This application provides an example of using the Flex Data Management Services with the Spring IoC container.

Note: The Flex Data Management Services leverage the Java Transaction API (JTA). If you are using Tomcat, or another servlet container that doesn’t provide a full implementation of the J2EE stack, you have to install a JTA implementation such as JOTM to run this example. Click here for more information.

Step 1: Copy the application files
Copy ProductAssembler.class from flex-spring-sdk\bin\flex\samples\spring\store to {context-root}\WEB-INF\classes\flex\samples\spring\store
Copy dms.mxml and Product.as from flex-spring-sdk\flex\dms to {context-root}\dms
Step 2: Register Spring Beans
Open ProductAssembler.java in a code editor. Notice that ProductAssembler has a dependency to a ProductDAO object.
Register the productAssemblerBean in applicationContext.xml as follows:
<bean id="productAssemblerBean" class="flex.samples.spring.store.ProductAssembler">
    <property name="productDAO" ref="productDAOBean"/>
</bean>Step 3: Configure the Flex Data Management Services Destination
Open data-management-config.xml in {context-root}\WEB-INF\flex.
Add the product destination as follows:
<destination id="product">
    <adapter ref="java-dao" />
    <properties>
        <source>productAssemblerBean</source>
        <factory>spring</factory>
        <metadata>
            <identity property="productId"/>
        </metadata>
    </properties>
</destination> Step 4: Run the Client Application
Open {context-root}\dms\dms.mxml in a code editor and familiarize yourself with the application.
Open a browser, access http://host:port/context-root/dms/dms.mxml, and test the application.
分享到:
评论

相关推荐

    Flex-Spring拦截器

    当我们在Flex-Spring集成中使用拦截器时,我们需要配置Spring的`HandlerInterceptor`或`WebBindingInitializer`接口,这些拦截器会在请求被处理前或处理后执行特定的代码。例如,我们可以通过以下步骤设置拦截器: ...

    flex-spring-blazeds demo

    本文将深入探讨如何将这三者完美结合,通过"flex-spring-blazeds demo"项目,展示一个高效、可扩展的Flex-Spring-BlazeDS集成解决方案。 首先,我们来看Flex在RIA中的核心作用。Flex基于ActionScript和MXML,为...

    Spring整合flex-Spring BlazeDS Integration-带项目demo

    Spring BlazeDS Integration是Spring框架的一个组件,它提供了与Adobe Flex的无缝集成,允许后端Java服务与前端Flex客户端进行通信。这个项目demo提供了实际操作的例子,帮助开发者理解和实现Spring与Flex的结合。 ...

    flex-spring blazeds integration基本框架搭建记录

    四、Spring集成 1. 配置Spring上下文:定义服务接口及其实现,使用Spring的@Service和@Repository注解标记为Bean。 2. 配置MessageBroker:在Spring上下文中,通过&lt;flex:message-broker&gt;标签配置BlazeDS的Message...

    整合flex-spring-mybatis

    ### 整合flex-spring-mybatis #### 一、项目背景及目标 本文档旨在详细介绍如何将Flex技术与Spring框架及MyBatis框架进行有效整合,以实现一个高性能且易于维护的企业级应用。通过整合这三种技术,可以充分利用Flex...

    Flex-Spring-JAVA-BLAZEDS.rar_Flex spring_flex_flex java

    压缩包内的“Flex Spring JAVA BLAZEDS.docx”文件很可能是详细的教程或者步骤指南,包括了创建Flex项目、集成Spring、配置BlazeDS、数据绑定和事件处理等方面的内容。可能涉及的具体知识点有: 1. Flex的基础知识...

    spring-flex-1.0.1.RELEASE

    Spring Flex 是 Spring 框架与 Adobe Flex 技术的结合体,旨在为开发者提供一个无缝集成的平台,用于构建富互联网应用程序(Rich Internet Applications,简称 RIA)。在本文中,我们将深入探讨 Spring Flex 1.0.1....

    spring-flex集成-demo

    在"spring-flex集成-demo"中,我们可以学习到以下关键知识点: 1. **Spring框架**:Spring是Java平台上的一个核心框架,它提供了依赖注入(DI)、面向切面编程(AOP)、事务管理等核心功能,以及对其他各种框架的...

    整合flex和spring常遇到的问题

    在进行Flex与Spring集成的过程中,经常会遇到配置上的问题。例如,在配置文件`services-config.xml`中设置URI时,可能会遇到以下情况: **问题描述:** 在`services-config.xml`中设置URI为: ```xml ...

    spring-flex-1.5.2

    Spring Flex 是一个集成 Adobe Flex 和 Spring 框架的项目,它使得开发人员可以利用 Flex 的富客户端功能和 Spring 的企业级服务后端相结合,构建强大的、可伸缩的Web应用程序。Spring Flex 提供了在 Flex 客户端与 ...

    spring-flex-1.5.0.M2-dist.zip

    Spring Flex是Spring框架与Adobe Flex集成的工具集,它为开发者提供了一种无缝的方式,将后端的Spring服务与前端的Flex应用相结合,从而创建功能丰富的、交互性强的Web应用程序。这个名为“spring-flex-1.5.0.M2-...

    flex_spring_hessian框架(Java部分)

    在Flex-Spring集成中,Spring作为服务提供者,为Flex客户端提供业务逻辑和数据。 Hessian是一种轻量级的二进制RPC协议,由Caucho公司开发。相比HTTP,Hessian减少了网络传输的数据量,提高了远程调用的性能。...

    spring整合flex所需jar包

    - Spring-BlazeDS Integration:这是一个Spring项目,它提供了与BlazeDS的集成,使得配置Spring服务为Flex客户端可用变得更加简单。 - Proxy服务:在Spring中定义Proxy服务,这些服务会被BlazeDS暴露,供Flex...

    上一篇的flex的远程对象调用,flex和spring集成分页的jar和截图

    描述中提到的“flex和spring集成分页”是指将Flex客户端与Spring框架结合,实现服务器端数据的分页展示。Spring是一个广泛使用的Java企业级应用开发框架,提供IoC(Inversion of Control)容器、数据访问、事务管理...

    spring-flex-reference

    ### Spring与Flex集成知识点 #### 一、Spring BlazeDS Integration 概览 **背景:** Spring框架自设计之初就致力于保持技术中立性,尤其是在处理客户端访问其核心服务时。这意味着Spring并不限定特定的前端技术栈...

    spring+mybatis+flex4框架组合,适合初学者

    Spring、MyBatis和Flex4是三个在软件开发领域中广泛应用的框架,它们各自负责不同的技术层面,而将它们组合起来可以构建出高效且用户体验良好的Web应用。下面将详细介绍这三个框架以及它们如何协同工作。 首先,...

    Flex4 spring整合实例

    在本整合实例中,我们将探讨如何将Adobe Flex4与Spring框架无缝集成,以实现更高效、更灵活的应用开发。 Flex4是Adobe Flex SDK的一个版本,它提供了强大的用户界面组件和动画效果,以及ActionScript 3.0编程语言的...

    Using Flex with Spring

    《使用Flex与Spring集成开发详解》 在现代Web应用程序开发中,Flex作为一款强大的富互联网应用(RIA)开发框架,以其丰富的用户界面和强大的交互能力备受开发者青睐。与此同时,Spring框架作为Java企业级应用的事实...

Global site tag (gtag.js) - Google Analytics