Adobe Flex是一套创建富客户端应用(RIAs)的框架.Flex生成的swf文件可以直接运行在Flash Player之中。相比较基于时间轴的Flash开发,Flex框架更适合那些用传统方式开发应用程序的开发人员。Flex应用可以使用Flex builder来开发。这套IDE是基于Eclipse平台开发的。Action Script3语言是用来访问数据和创建用户接口组件的。Flex框架也用到了一种XML语言叫做MXML,它是用来简化Flex开发和布局的。
Spring是目前最受欢迎的创建企业级应用的Java框架。不像传统的J2EE开发,Spring提供了轻量级的容器。使用Spring会使应用的测试和开发更为简单。虽然Spring依赖注入的功能最出名,但是它也提供了其他服务端企业程序所需要的功能。如安全和事务处理。
Flex技术本身和Java就有很强的关联性,它提供了一个基于Eclipse的IDE和BlazeDS.BlazeDS是个基于服务端的Java远程调用和Web消息的一个开源的技术。有许多应用都是以Java为后端处理的。Flex用于前端。由于Java和Flex一起频繁的使用。人们很容易就想到Flex和Spring的整合。有许多企业和组织已经着手开始使用Flex作为用户接口了。在2008年末,Spring社区已经着手Spring BlazeDS整合的项目。为Java和Spring添加更好的Flex支持。
默认的情况下BlazeDS创建了一个服务端Java对象的实例,用它们来完成远程对象的请求。但是这种方法并不适用于Spring.因为整个框架使用的服务的对象都是用Spring容器所创建的。Spring和BlazeDS的整合,Flex就可以使用Spring中的对象了。
为了使用BlazeDS,服务端的Java应用应打包为WAR文件。这部分的代码实例是使用Eclipse来创建和编辑的。步骤如下
public class MyEntity { private String frstName; private String lastName; private String emailAddress; public String getFirstName() { return frstName; } public void setFirstName(String frstName) { this.frstName = frstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmailAddress() { return emailAddress; } public void setEmailAddress(String emailAddress) { this.emailAddress = emailAddress; } }
import java.util.List; public interface MyService { public List<MyEntity> getMyEntities(); }
import java.util.ArrayList; import java.util.List; public class MyServiceImpl implements MyService { public List<MyEntity> getMyEntities() { List<MyEntity> list = new ArrayList<MyEntity>(); MyEntity entity = new MyEntity(); entity.setFirstName("Hello"); entity.setLastName("World"); entity.setEmailAddress("[email protected]"); list.add(entity); MyEntity entity2 = new MyEntity(); entity2.setFirstName("Hello"); entity2.setLastName("Space"); entity2.setEmailAddress("[email protected]"); list.add(entity2); MyEntity entity3 = new MyEntity(); entity3.setFirstName("Hello"); entity3.setLastName("Neighbor"); entity3.setEmailAddress("[email protected]"); list.add(entity3); return list; } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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"> <!-- Spring Beans’s --> <bean id="myService" class="MyServiceImpl" /> </beans>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>test-server</display-name> <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>/WEB-INF/application-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Map /spring/* requests to the DispatcherServlet --> <servlet-mapping> <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> <url-pattern>/spring/*</url-pattern> </servlet-mapping> </web-app>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:flex="http://www.springframework.org/schema/flex" 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"> <!-- Spring Beans’s --> <bean id="myService" class="MyServiceImpl" /> <!-- Simplest possible message broker --> <flex:message-broker/> <!-- exposes myService as BlazeDS destination --> <flex:remoting-destination ref="myService" /> </beans>
<?xml version="1.0" encoding="UTF-8"?> <services-config> <services> <default-channels> <channel ref="my-amf" /> </default-channels> </services> <channels> <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" /> </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> </channels> </services-config>
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="srv.getMyEntities()"> <mx:AMFChannel id="myamf" uri="/test-server/spring/messagebroker/amf"/> <mx:ChannelSet id="channelSet" channels="{[myamf]}"/> <mx:RemoteObject id="srv" destination="myService" channelSet="{channelSet}"/> <mx:DataGrid dataProvider="{srv.getMyEntities.lastResult}"/> </mx:Application>
为了在Flex builder中调试运行。可以做如下设置