天天看點

Flex Spring整合包

  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來建立和編輯的。步驟如下 設定服務端的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/B lazeDS/

  Spring架構:http://www.springsource.org/download

  Spring BlazeDS整合:http://www.springsource.org/spring-flex

  ANTLR3.0:http://www.antlr.org/download.html

  首先設定服務端的JAVA工程,用blazeds.war(在blazeds的壓縮包中)建立一個WEB工程。步驟如下

  Choose File>import

  選擇WAR選項。指定blazedsWAR檔案的位置。輸入工程名test-server

  點選完成

  現在就可以建立一個伺服器來運作這個WEB應用。

  File>New>Other

  選擇Server>Server

  點選Next

  選擇Apache>Tomcat6 Server

  點選Next

  指定Tomcat的安裝位置以及JRE(5以上版本)

  點選Next

  在Availble Projects list中選擇test-server

  點選Add添加到Configured Project list

  點選Finish

  接下來就可以建立Java類了。這個類在Java和Flex之間傳輸

  Java代碼 type="application/x-shockwave-flash" width="14" height="15" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" flashvars="clipboard=public%20class%20MyEntity%20% 7B%0A%20private%20String%20frstName%3B%0A%20private %20String%20lastName%3B%0A%20private%20String%20ema ilAddress%3B%0A%20%0A%20public%20String%20getFirstN ame()%20%7B%0A%20%20return%20frstName%3B%0A%20%7D%0 A%20public%20void%20setFirstName(String%20frstName) %20%7B%0A%20%20this.frstName%20%3D%20frstName%3B%0A %20%7D%0A%20public%20String%20getLastName()%20%7B%0 A%20%20return%20lastName%3B%0A%20%7D%0A%20public%20 void%20setLastName(String%20lastName)%20%7B%0A%20%2 0this.lastName%20%3D%20lastName%3B%0A%20%7D%0A%20pu blic%20String%20getEmailAddress()%20%7B%0A%20%20ret urn%20emailAddress%3B%0A%20%7D%0A%20public%20void%2 0setEmailAddress(String%20emailAddress)%20%7B%0A%20 %20this.emailAddress%20%3D%20emailAddress%3B%0A%20% 7D%0A%7D" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflash player" width="14" height="15">

  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; } }

  Java代碼 type="application/x-shockwave-flash" width="14" height="15" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" flashvars="clipboard=import%20java.util.List%3B%0A public%20interface%20MyService%20%7B%0A%20public%20 List%3CMyEntity%3E%20getMyEntities()%3B%0A%7D" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflash player" width="14" height="15">

  Java代碼 type="application/x-shockwave-flash" width="14" height="15" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" flashvars="clipboard=import%20java.util.ArrayList% 3B%20%20%0Aimport%20java.util.List%3B%20%20%0Apubli c%20class%20MyServiceImpl%20implements%20MyService% 20%7B%20%20%0A%20public%20List%3CMyEntity%3E%20getM yEntities()%20%7B%20%20%0A%20%20List%3CMyEntity%3E% 20list%20%3D%20new%20ArrayList%3CMyEntity%3E()%3B%2 0%20%0A%20%20MyEntity%20entity%20%3D%20new%20MyEnti ty()%3B%20%20%0A%20%20entity.setFirstName(%22Hello% 22)%3B%20%20%0A%20%20entity.setLastName(%22World%22 )%3B%20%20%0A%20%20entity.setEmailAddress(%22hello% 40world.com%22)%3B%20%20%0A%20%20list.add(entity)%3 B%20%20%0A%20%20MyEntity%20entity2%20%3D%20new%20My Entity()%3B%20%20%0A%20%20entity2.setFirstName(%22H ello%22)%3B%20%20%0A%20%20entity2.setLastName(%22Sp ace%22)%3B%20%20%0A%20%20entity2.setEmailAddress(%2 2hello%40space.com%22)%3B%20%20%0A%20%20list.add(en tity2)%3B%20%20%0A%20%20MyEntity%20entity3%20%3D%20 new%20MyEntity()%3B%20%20%0A%20%20entity3.setFirstN ame(%22Hello%22)%3B%20%20%0A%20%20entity3.setLastNa me(%22Neighbor%22)%3B%20%20%0A%20%20entity3.setEmai lAddress(%22hello%40neighbor.com%22)%3B%20%20%0A%20 %20list.add(entity3)%3B%20%20%0A%20%20return%20list %3B%20%20%0A%20%7D%20%20%0A%7D%20%20%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflash player" width="14" height="15">

  import java.util.ArrayList;

  import java.util.List;

  public class MyServiceImpl implements MyService {

  public List getMyEntities() {

  List list = new ArrayList();

  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;

  } }

  import java.util.ArrayList; import java.util.List; public class MyServiceImpl implements MyService { public List getMyEntities() { List list = new ArrayList(); 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; } }

  這三個類對于例子足夠了。在實戰中,這個服務類可能要連接配接到資料庫。為了友善我們學習,這個例子中就傳回的是個list的寫死了。

  基本的java工程算是完工了。。

  接下來我們要做Spring的配置了。

  把Spring的庫以及Spring BlazeDS整合的庫,還有ANTLR庫檔案放到項目/WEB-INF/lib下。

  建立一個Spring配置檔案。滑鼠右鍵點選WebContent/WEB-INF以及選擇New>File,檔案名輸入application-config.xml.點選完成。配置檔案内容如下。

  Xml代碼 type="application/x-shockwave-flash" width="14" height="15" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%3Fxml%20version%3D%221.0% 22%20encoding%3D%22UTF-8%22%3F%3E%20%20%0A%3Cbeans% 20xmlns%3D%22http%3A%2F%2Fwww.springframework.org%2 Fschema%2Fbeans%22%20%20%0A%20xmlns%3Axsi%3D%22http %3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema-instance%22% 20%20%0A%20xsi%3AschemaLocation%3D%22%20%20%0A%20ht tp%3A%2F%2Fwww.springframework.org%2Fschema%2Fbeans %20%20%0A%20http%3A%2F%2Fwww.springframework.org%2F schema%2Fbeans%2Fspring-beans-2.5.xsd%22%3E%20%20%0 A%20%3C!--%20Spring%20Beans%E2%80%99s%20--%3E%20%20 %0A%20%3Cbean%20id%3D%22myService%22%20class%3D%22M yServiceImpl%22%20%2F%3E%20%20%0A%3C%2Fbeans%3E%20% 20%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflash player" width="14" height="15">

  

  

  

  

  

  懂Spring的人看這配置最熟悉不過了。。

  通過這一步,已經有了一個BlazeDS預設配置的Java web工程。下面我們就更改BlazeDS預設配置,去使用新建立的Spring中的bean.

  為了配置Spring BlazeDS的整合,更新web.xml。

  Xml代碼 type="application/x-shockwave-flash" width="14" height="15" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%3Fxml%20version%3D%221.0% 22%20encoding%3D%22UTF-8%22%3F%3E%0A%3Cweb-app%20xm lns%3Axsi%3D%22http%3A%2F%2Fwww.w3.org%2F2001%2FXML Schema-instance%22%0A%09xmlns%3D%22http%3A%2F%2Fjav a.sun.com%2Fxml%2Fns%2Fjavaee%22%20xmlns%3Aweb%3D%2 2http%3A%2F%2Fjava.sun.com%2Fxml%2Fns%2Fjavaee%2Fwe b-app_2_5.xsd%22%0A%09xsi%3AschemaLocation%3D%22htt p%3A%2F%2Fjava.sun.com%2Fxml%2Fns%2Fjavaee%20http%3 A%2F%2Fjava.sun.com%2Fxml%2Fns%2Fjavaee%2Fweb-app_2 _5.xsd%22%0A%09id%3D%22WebApp_ID%22%20version%3D%22 2.5%22%3E%0A%09%3Cdisplay-name%3Etest-server%3C%2Fd isplay-name%3E%0A%09%3Cservlet%3E%0A%09%09%3Cservle t-name%3ESpring%20MVC%20Dispatcher%20Servlet%3C%2Fs ervlet-name%3E%0A%09%09%3Cservlet-class%3Eorg.sprin gframework.web.servlet.DispatcherServlet%3C%2Fservl et-class%3E%0A%09%09%3Cinit-param%3E%0A%09%09%09%3C param-name%3EcontextConfigLocation%3C%2Fparam-name% 3E%0A%09%09%09%3Cparam-value%3E%2FWEB-INF%2Fapplica tion-config.xml%3C%2Fparam-value%3E%0A%09%09%3C%2Fi nit-param%3E%0A%09%09%3Cload-on-startup%3E1%3C%2Flo ad-on-startup%3E%0A%09%3C%2Fservlet%3E%0A%09%3C!--% 20Map%20%2Fspring%2F*%20requests%20to%20the%20Dispa tcherServlet%20--%3E%0A%09%3Cservlet-mapping%3E%0A% 09%09%3Cservlet-name%3ESpring%20MVC%20Dispatcher%20 Servlet%3C%2Fservlet-name%3E%0A%09%09%3Curl-pattern %3E%2Fspring%2F*%3C%2Furl-pattern%3E%0A%09%3C%2Fser vlet-mapping%3E%0A%3C%2Fweb-app%3E%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflash player" width="14" height="15">

  

  

  test-server

  

  Spring MVC Dispatcher Servlet

  org.springframework.web.servlet.DispatcherServlet

  

  contextConfigLocation

  /WEB-INF/application-config.xml

  

  1

  

  

  

  Spring MVC Dispatcher Servlet

  /spring/*

  

  

   test-server Spring MVC Dispatcher Servlet org.springframework.web.servlet.Dispa tcherServlet contextConfigLocation /WEB-INF/application-config.xml 1 Spring MVC Dispatcher Servlet /spring/*

  建立的Servlet可以處理這個請求,http://localhost:8080/test-server/spring

  這是通路BlazeDS的基本的URL。當然這也是Spring标準的DispatcherServlet.

  現在已經把Spring整合到Java web工程中了。要整合BlazeDS,就要修改下Spring的配置檔案。

  application-config.xml檔案如下

  Xml代碼 type="application/x-shockwave-flash" width="14" height="15" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%3Fxml%20version%3D%221.0% 22%20encoding%3D%22UTF-8%22%3F%3E%0A%3Cbeans%20xmln s%3D%22http%3A%2F%2Fwww.springframework.org%2Fschem a%2Fbeans%22%0A%20xmlns%3Axsi%3D%22http%3A%2F%2Fwww .w3.org%2F2001%2FXMLSchema-instance%22%0A%20xmlns%3 Aflex%3D%22http%3A%2F%2Fwww.springframework.org%2Fs chema%2Fflex%22%0A%20xsi%3AschemaLocation%3D%22%0A% 20http%3A%2F%2Fwww.springframework.org%2Fschema%2Fb eans%0A%20http%3A%2F%2Fwww.springframework.org%2Fsc hema%2Fbeans%2Fspring-beans-2.5.xsd%0A%20http%3A%2F %2Fwww.springframework.org%2Fschema%2Fflex%0A%20htt p%3A%2F%2Fwww.springframework.org%2Fschema%2Fflex%2 Fspring-flex-1.0.xsd%22%3E%20%20%0A%20%3C!--%20Spri ng%20Beans%E2%80%99s%20--%3E%0A%20%3Cbean%20id%3D%2 2myService%22%20class%3D%22MyServiceImpl%22%20%2F%3 E%0A%20%3C!--%20Simplest%20possible%20message%20bro ker%20--%3E%20%0A%20%3Cflex%3Amessage-broker%2F%3E% 0A%20%20%3C!--%20exposes%20myService%20as%20BlazeDS %20destination%20--%3E%0A%20%3Cflex%3Aremoting-dest ination%20ref%3D%22myService%22%20%2F%3E%0A%3C%2Fbe ans%3E%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflash player" width="14" height="15">

  

  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">

  

  

  

  flex:message-broker/>

  

  flex:remoting-destination ref="myService" />

  

   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"> flex:message-broker/> flex:remoting-destination ref="myService" />

  通過配置,使BlazeDS接口開放。首先要添加Flex的namespace。添加之後,使用message-broker标簽建立 MessageBrokerFactoryBean。看下配置隻是個簡單标簽。配置是預設的。要確定WEB-INF/flex 下有service-config.xml這個配置檔案。remoting-destination标簽使Spring bean變為遠端目标。

  接下來修改預設的BlazeDS service-config.xml檔案。代碼如下

  Xml代碼 type="application/x-shockwave-flash" width="14" height="15" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%3Fxml%20version%3D%221.0% 22%20encoding%3D%22UTF-8%22%3F%3E%0A%3Cservices-con fig%3E%0A%09%3Cservices%3E%0A%09%09%3Cdefault-chann els%3E%0A%09%09%09%3Cchannel%20ref%3D%22my-amf%22%2 0%2F%3E%0A%09%09%3C%2Fdefault-channels%3E%0A%09%3C% 2Fservices%3E%0A%09%3Cchannels%3E%0A%09%09%3Cchanne l-definition%20id%3D%22my-amf%22%0A%09%09%09class%3 D%22mx.messaging.channels.AMFChannel%22%3E%0A%09%09 %09%3Cendpoint%0A%09%09%09%09url%3D%22http%3A%2F%2F %7Bserver.name%7D%3A%7Bserver.port%7D%2F%7Bcontext. root%7D%2Fspring%2Fmessagebroker%2Famf%22%0A%09%09% 09%09class%3D%22flex.messaging.endpoints.AMFEndpoin t%22%20%2F%3E%0A%09%09%3C%2Fchannel-definition%3E%0 A%09%09%3Cchannel-definition%20id%3D%22my-polling-a mf%22%0A%09%09%09class%3D%22mx.messaging.channels.A MFChannel%22%3E%0A%09%09%09%3Cendpoint%0A%09%09%09% 09url%3D%22http%3A%2F%2F%7Bserver.name%7D%3A%7Bserv er.port%7D%2F%7Bcontext.root%7D%2Fspring%2Fmessageb roker%2Famfpolling%22%0A%09%09%09%09class%3D%22flex .messaging.endpoints.AMFEndpoint%22%20%2F%3E%0A%09% 09%09%3Cproperties%3E%0A%09%09%09%09%3Cpolling-enab led%3Etrue%3C%2Fpolling-enabled%3E%0A%09%09%09%09%3 Cpolling-interval-seconds%3E4%3C%2Fpolling-interval -seconds%3E%0A%09%09%09%3C%2Fproperties%3E%0A%09%09 %3C%2Fchannel-definition%3E%0A%09%3C%2Fchannels%3E% 0A%3C%2Fservices-config%3E%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflash player" width="14" height="15">

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  true

  4

  

  

  

  

   true 4

  看一下 endpoint标簽的的url.唯一可以修改的就是content.root之後的spring.所有遠端目标配置都應該配置在application-config.xml檔案中。

  File>New>Other

  選擇Flex Project

  填寫工程名稱test-flex

  用預設的位址

  選擇Web application(運作在Flash player)

  Application Type 選擇None

  點選Next

  指定Output的檔案夾。如 C:\workspace\test-server\WebContent\

  點選Finish

  Xml代碼 type="application/x-shockwave-flash" width="14" height="15" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" src="http://xiayuanfeng.javaeye.com/javascripts/sy ntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%3Fxml%20version%3D%221.0% 22%20encoding%3D%22utf-8%22%3F%3E%0A%3Cmx%3AApplica tion%20xmlns%3Amx%3D%22http%3A%2F%2Fwww.adobe.com%2 F2006%2Fmxml%22%20%0A%09layout%3D%22absolute%22%20% 0A%09creationComplete%3D%22srv.getMyEntities()%22%3 E%0A%09%3Cmx%3AAMFChannel%20id%3D%22myamf%22%20uri% 3D%22%2Ftest-server%2Fspring%2Fmessagebroker%2Famf% 22%2F%3E%20%20%0A%20%3Cmx%3AChannelSet%20id%3D%22ch annelSet%22%20channels%3D%22%7B%5Bmyamf%5D%7D%22%2F %3E%20%20%0A%20%3Cmx%3ARemoteObject%20id%3D%22srv%2 2%20%0A%20%20%20destination%3D%22myService%22%20cha nnelSet%3D%22%7BchannelSet%7D%22%2F%3E%20%20%0A%20% 3Cmx%3ADataGrid%20dataProvider%3D%22%7Bsrv.getMyEnt ities.lastResult%7D%22%2F%3E%0A%3C%2Fmx%3AApplicati on%3E%0A%0A" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflash player" width="14" height="15">

  

  layout="absolute"

  creationComplete="srv.getMyEntities()">

  

  

  

  

  

   layout="absolute" creationComplete="srv.getMyEntities()">

  以上代碼的AMFChannel通路了Spring的服務。

  要注意的是RemoteObject标簽中的destination的destination要和spring的application-config.xml中remote-service标簽的ref所設定的值保持一緻。通過代碼可以看出Flex并沒有包含關于Spring的東西。編寫Flex的

  開發人員不必知道關于Spring的知識。

  要更新test-server的程式。可以Refresh這個工程。

  下面測試一下。啟動Tomcat.http://localhost:8080/test-server/main.html

  

  為了在Flex builder中調試運作。可以做如下設定 之後直接運作就可以了。整合完成。