原文地址:http://www.javaeye.com/topic/392836
关于Flex
Adobe Flex是一套创建富客户端应用(RIAs)的框架.Flex生成的swf文件可以直接运行在Flash Player之中。相比较基于时间轴的Flash开发,Flex框架更适合那些用传统方式开发应用程序的开发人员。Flex应用可以使用Flex builder来开发。这套IDE是基于Eclipse平台开发的。Action Script3语言是用来访问数据和创建用户接口组件的。Flex框架也用到了一种XML语言叫做MXML,它是用来简化Flex开发和布局的。
关于Spring
Spring是目前最受欢迎的创建企业级应用的Java框架。不像传统的J2EE开发,Spring提供了轻量级的容器。使用Spring会使应用的测试和开发更为简单。虽然Spring依赖注入的功能最出名,但是它也提供了其他服务端企业程序所需要的功能。如安全和事务处理。
为什么使用Flex和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中的对象了。
整合Flex和Spring
为了使用BlazeDS,服务端的Java应用应打包为WAR文件。这部分的代码实例是使用Eclipse来创建和编辑的。步骤如下
- 设置服务端的BlazeDS Java工程以及Spring框架。
- 在BlazeDS中配置Spring的bean
- 写一个Flex应用去使用Spring/BlazeDS的服务。
- Eclipse3.4(J2EE版本):http://www.eclipse.org/downloads/
- Flex Builder3:http://www.adobe.com/cfusion/entitlement/index.cfm?e=flex3email&sdid=EOZPI
- Tomcat 6:http://tomcat.apache.org/
- BlazeDS:http://opensource.adobe.com/wiki/display/blazeds/BlazeDS/
- Spring框架:http://www.springsource.org/download
- Spring BlazeDS整合:http://www.springsource.org/spring-flex
- ANTLR3.0:http://www.antlr.org/download.html
- Choose File>import
- 选择WAR选项。指定blazedsWAR文件的位置。输入工程名test-server
- 点击完成
- File>New>Other
- 选择Server>Server
- 点击Next
- 选择Apache>Tomcat6 Server
- 点击Next
- 指定Tomcat的安装位置以及JRE(5以上版本)
- 点击Next
- 在Availble Projects list中选择test-server
- 点击Add添加到Configured Project list
- 点击Finish
- 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;
- }
- }
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.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("hello@world.com");
- list.add(entity);
- MyEntity entity2 = new MyEntity();
- entity2.setFirstName("Hello");
- entity2.setLastName("Space");
- entity2.setEmailAddress("hello@space.com");
- list.add(entity2);
- MyEntity entity3 = new MyEntity();
- entity3.setFirstName("Hello");
- entity3.setLastName("Neighbor");
- entity3.setEmailAddress("hello@neighbor.com");
- list.add(entity3);
- return list;
- }
- }
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("hello@world.com");
list.add(entity);
MyEntity entity2 = new MyEntity();
entity2.setFirstName("Hello");
entity2.setLastName("Space");
entity2.setEmailAddress("hello@space.com");
list.add(entity2);
MyEntity entity3 = new MyEntity();
entity3.setFirstName("Hello");
entity3.setLastName("Neighbor");
entity3.setEmailAddress("hello@neighbor.com");
list.add(entity3);
return list;
}
}
- 把Spring的库以及Spring BlazeDS整合的库,还有ANTLR库文件放到项目/WEB-INF/lib下。
- 创建一个Spring配置文件。鼠标右键点击WebContent/WEB-INF以及选择New>File,文件名输入application-config.xml.点击完成。配置文件内容如下。
- <?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>
- File>New>Other
- 选择Flex Project
- 填写工程名称test-flex
- 用默认的地址
- 选择Web application(运行在Flash player)
- Application Type 选择None
- 点击Next
- 指定Output的文件夹。如 C:/workspace/test-server/WebContent/
- 点击Finish
- <?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>