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項目名稱
附件是兩份web.xml。
FacadeAccessor首先獲取應用context,然後從context得到PetStoreFacade業務對象。
- // Decompiled by DJ v2.9.9.60 Copyright 2000 Atanas Neshkov Date: 2007-05-22 22:12:45
- // Home Page : http://members.fortunecity.com/neshkov/dj.html - Check often for new version!
- // Decompiler options: packimports(3)
- // Source File Name: FacadeAccessor.java
- package org.springframework.samples.jpetstore.domain.logic.flex;
- import java.io.*;
- import java.util.List;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.samples.jpetstore.domain.*;
- import org.springframework.samples.jpetstore.domain.logic.PetStoreFacade;
- import org.springframework.samples.jpetstore.domain.logic.flex.exception.NotFoundBeanDifinitionException;
- import org.springframework.samples.jpetstore.domain.logic.flex.exception.OutputBeanDifinitionException;
- public class FacadeAccessor {
- public FacadeAccessor() {
- petStoreFacade = null;
- init();
- }
- private void init() {
- try {
- getPetStoreFacade();
- } catch (Exception exception) {
- }
- }
- private PetStoreFacade getPetStoreFacade()
- throws NotFoundBeanDifinitionException,
- OutputBeanDifinitionException {
- if (petStoreFacade == null) {
- ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
- "/applicationContext*.xml");
- String beanDefinitionNames[] = ctx.getBeanDefinitionNames();
- if (beanDefinitionNames == null || beanDefinitionNames.length < 1)
- throw new NotFoundBeanDifinitionException(
- "Not found bean difinitions.");
- petStoreFacade = (PetStoreFacade) ctx.getBean("petStore");
- }
- return petStoreFacade;
- }
- private void outputBeanDefinitionNames(String beanDefinitionNames[])
- throws OutputBeanDifinitionException {
- if (beanDefinitionNames != null && beanDefinitionNames.length > 0) {
- DataOutputStream out = null;
- try {
- out = new DataOutputStream(new BufferedOutputStream(
- new FileOutputStream("c:\\beandifinitions.txt")));
- for (int i = 0; i < beanDefinitionNames.length; i++)
- out.writeBytes((new StringBuilder(String
- .valueOf(beanDefinitionNames[i]))).append("\r\n")
- .toString());
- out.close();
- } catch (IOException e) {
- throw new OutputBeanDifinitionException((new StringBuilder(
- "error occurs when output bean difinitions to file,"))
- .append(e.getMessage()).toString());
- }
- }
- }
- public Account getAccount(String username) {
- return petStoreFacade.getAccount(username);
- }
- public Account getAccount(String username, String password) {
- return petStoreFacade.getAccount(username, password);
- }
- public void insertAccount(Account account) {
- petStoreFacade.insertAccount(account);
- }
- public void updateAccount(Account account) {
- petStoreFacade.updateAccount(account);
- }
- public List getUsernameList() {
- return petStoreFacade.getUsernameList();
- }
- public List getCategoryList() {
- return petStoreFacade.getCategoryList();
- }
- public Category getCategory(String categoryId) {
- return petStoreFacade.getCategory(categoryId);
- }
- public List getProductListByCategory(String categoryId) {
- return petStoreFacade.getProductListByCategory(categoryId);
- }
- public List searchProductList(String keywords) {
- return petStoreFacade.searchProductList(keywords);
- }
- public Product getProduct(String productId) {
- return petStoreFacade.getProduct(productId);
- }
- public List getItemListByProduct(String productId) {
- return petStoreFacade.getItemListByProduct(productId);
- }
- public Item getItem(String itemId) {
- return petStoreFacade.getItem(itemId);
- }
- public boolean isItemInStock(String itemId) {
- return petStoreFacade.isItemInStock(itemId);
- }
- public void insertOrder(Order order) {
- petStoreFacade.insertOrder(order);
- }
- public Order getOrder(int orderId) {
- return petStoreFacade.getOrder(orderId);
- }
- public List getOrdersByUsername(String username) {
- return petStoreFacade.getOrdersByUsername(username);
- }
- private static final boolean output = false;
- private static final String OUTPUT_BEANDIFINITIONS_PATHFILE = "c:\\beandifinitions.txt";
- private static final String APPLICATION_CONTEXT_XML = "/applicationContext*.xml";
- private PetStoreFacade petStoreFacade;
- }