FlexPetStore--集成Flex和Spring (2)

Flex部署在Flex Builder幫助中有詳細說明(如部署目錄結構)。遺憾的是沒有例子介紹Flex如何集成Spring。本文嘗試解釋這部分內容。由於機器重裝,導致FlexPetStore的項目丟失,幸好是覆蓋安裝留下了可運行的WAR。

FlexPetStore通過RemoteObject技術訪問JPetStoreFacade業務對象(引入一個FacadeAccessor對象),因此使用了Flex Data Service 2.0。

大致流程:

Flex (ActionScript )<--> AMF協議(AMF協議編碼)<-->(解碼)<-->FacadeAccessor (Java) <--> PetStoreFacade (Java)<--> ...

看了一下 FDS 2.0 提供的幾個應用WAR,發現只要按照Flex的部署思路,在現有WAR基礎上適當修改就可以集成Flex和Spring。首先需要把flex.war該名爲flexpetstore.war,現在按照一下介紹逐步修改。

Spring集成

1. 加入Lib庫。把spring.jar和jpetsotre.jar放到 flexpetstore\WEB-INF\lib下,這是運行Spring和JPetstore的必備包。
2. 修改flexpetstore.war的web.xml,經過一番對比和試驗找到兩個段落需要修改。
   a. 添加web應用根目錄和Spring應用上下文以及數據訪問資源配置。
   b. 添加Remoting、web service。struts部分極有可能多餘,因爲沒有action來Request。有待測試。
   c. 修改welcome-file-list,FlexPetStore.html是FlexPetStore項目名稱
  

到此爲止,集成Spring配置基本完成。還有一部分服務端的Java代碼作爲RemoteObject供Flex訪問,下一部分再講解。

附件是兩份web.xml。

FacadeAccessor首先獲取
應用context,然後從context得到PetStoreFacade業務對象。
java 代碼
 
  1. // Decompiled by DJ v2.9.9.60 Copyright 2000 Atanas Neshkov  Date: 2007-05-22 22:12:45  
  2. // Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!  
  3. // Decompiler options: packimports(3)   
  4. // Source File Name:   FacadeAccessor.java  
  5.   
  6. package org.springframework.samples.jpetstore.domain.logic.flex;  
  7.   
  8. import java.io.*;  
  9. import java.util.List;  
  10. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  11. import org.springframework.samples.jpetstore.domain.*;  
  12. import org.springframework.samples.jpetstore.domain.logic.PetStoreFacade;  
  13. import org.springframework.samples.jpetstore.domain.logic.flex.exception.NotFoundBeanDifinitionException;  
  14. import org.springframework.samples.jpetstore.domain.logic.flex.exception.OutputBeanDifinitionException;  
  15.   
  16. public class FacadeAccessor {  
  17.   
  18.     public FacadeAccessor() {  
  19.         petStoreFacade = null;  
  20.         init();  
  21.     }  
  22.   
  23.     private void init() {  
  24.         try {  
  25.             getPetStoreFacade();  
  26.         } catch (Exception exception) {  
  27.         }  
  28.     }  
  29.   
  30.     private PetStoreFacade getPetStoreFacade()  
  31.             throws NotFoundBeanDifinitionException,  
  32.             OutputBeanDifinitionException {  
  33.         if (petStoreFacade == null) {  
  34.             ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(  
  35.                     "/applicationContext*.xml");  
  36.             String beanDefinitionNames[] = ctx.getBeanDefinitionNames();  
  37.             if (beanDefinitionNames == null || beanDefinitionNames.length < 1)  
  38.                 throw new NotFoundBeanDifinitionException(  
  39.                         "Not found bean difinitions.");  
  40.             petStoreFacade = (PetStoreFacade) ctx.getBean("petStore");  
  41.         }  
  42.         return petStoreFacade;  
  43.     }  
  44.   
  45.     private void outputBeanDefinitionNames(String beanDefinitionNames[])  
  46.             throws OutputBeanDifinitionException {  
  47.         if (beanDefinitionNames != null && beanDefinitionNames.length > 0) {  
  48.             DataOutputStream out = null;  
  49.             try {  
  50.                 out = new DataOutputStream(new BufferedOutputStream(  
  51.                         new FileOutputStream("c:\\beandifinitions.txt")));  
  52.                 for (int i = 0; i < beanDefinitionNames.length; i++)  
  53.                     out.writeBytes((new StringBuilder(String  
  54.                             .valueOf(beanDefinitionNames[i]))).append("\r\n")  
  55.                             .toString());  
  56.   
  57.                 out.close();  
  58.             } catch (IOException e) {  
  59.                 throw new OutputBeanDifinitionException((new StringBuilder(  
  60.                         "error occurs when output bean difinitions to file,"))  
  61.                         .append(e.getMessage()).toString());  
  62.             }  
  63.         }  
  64.     }  
  65.   
  66.     public Account getAccount(String username) {  
  67.         return petStoreFacade.getAccount(username);  
  68.     }  
  69.   
  70.     public Account getAccount(String username, String password) {  
  71.         return petStoreFacade.getAccount(username, password);  
  72.     }  
  73.   
  74.     public void insertAccount(Account account) {  
  75.         petStoreFacade.insertAccount(account);  
  76.     }  
  77.   
  78.     public void updateAccount(Account account) {  
  79.         petStoreFacade.updateAccount(account);  
  80.     }  
  81.   
  82.     public List getUsernameList() {  
  83.         return petStoreFacade.getUsernameList();  
  84.     }  
  85.   
  86.     public List getCategoryList() {  
  87.         return petStoreFacade.getCategoryList();  
  88.     }  
  89.   
  90.     public Category getCategory(String categoryId) {  
  91.         return petStoreFacade.getCategory(categoryId);  
  92.     }  
  93.   
  94.     public List getProductListByCategory(String categoryId) {  
  95.         return petStoreFacade.getProductListByCategory(categoryId);  
  96.     }  
  97.   
  98.     public List searchProductList(String keywords) {  
  99.         return petStoreFacade.searchProductList(keywords);  
  100.     }  
  101.   
  102.     public Product getProduct(String productId) {  
  103.         return petStoreFacade.getProduct(productId);  
  104.     }  
  105.   
  106.     public List getItemListByProduct(String productId) {  
  107.         return petStoreFacade.getItemListByProduct(productId);  
  108.     }  
  109.   
  110.     public Item getItem(String itemId) {  
  111.         return petStoreFacade.getItem(itemId);  
  112.     }  
  113.   
  114.     public boolean isItemInStock(String itemId) {  
  115.         return petStoreFacade.isItemInStock(itemId);  
  116.     }  
  117.   
  118.     public void insertOrder(Order order) {  
  119.         petStoreFacade.insertOrder(order);  
  120.     }  
  121.   
  122.     public Order getOrder(int orderId) {  
  123.         return petStoreFacade.getOrder(orderId);  
  124.     }  
  125.   
  126.     public List getOrdersByUsername(String username) {  
  127.         return petStoreFacade.getOrdersByUsername(username);  
  128.     }  
  129.   
  130.     private static final boolean output = false;  
  131.   
  132.     private static final String OUTPUT_BEANDIFINITIONS_PATHFILE = "c:\\beandifinitions.txt";  
  133.   
  134.     private static final String APPLICATION_CONTEXT_XML = "/applicationContext*.xml";  
  135.   
  136.     private PetStoreFacade petStoreFacade;  
  137. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章