随着Flex应用的越来越多, 我接触大部分项目都或多或少的使用了Flex, 经过一段时间的积累, 形成了一个我个人用着比较顺手的架构.
下载
项目模板, 集成了BlazeDS, 可以直接使用).
使用Spring整合整个项目. Spring-flex集成BlazeDS, Spring托管其他web框架(Struts, Webwork ...).
项目目录结构如下.
src 下 applicationContext.xml 为核心业务的配置文件.
WEB-INF/config下为视图层配置文件.
使用数据访问对象(DAO)模式隔离持久层.
核心业务在服务层(Service),使用Facade模式对外提供统一的接口.
test源文件夹放 单元测试和集成测试的代码, 很容易的实现 核心业务代码 和 测试代码的分离(不同的源码树), 同时又使 测试类 和 被测试类在相同包(同样的package). applicationContext-test.xml 为支撑测试用的配置文件.
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<!-- Http Flex Session attribute and binding listener support -->
<listener>
<listener-class>flex.messaging.HttpFlexSession</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<servlet>
<servlet-name>flex</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/applicationContext-flex.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>web</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/applicationContext-web.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>flex</servlet-name>
<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>web</servlet-name>
<url-pattern>/page/*</url-pattern>
</servlet-mapping>
通过ContextLoaderListener初始化核心业务.
通过 Spring Web MVC, 分离 Flex 和其他 视图. flex 为Flex提供服务, web 为其他框架提供集成.
applicationContext.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="myContactsService" class="com.nealmi.mycontacts.service.MyContactsServiceImpl"></bean>
<bean id="contactDao" class="com.nealmi.mycontacts.dao.impl.JdbcContactDao"></bean>
</beans>
applicationContext-flex.xml 导出flex服务.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:flex="http://www.springframework.org/schema/flex" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">
<flex:message-broker></flex:message-broker>
<flex:remoting-destination ref="myContactsService" />
</beans>
applicationContext-web.xml 这里托管了 struts(注释部分配置).
<?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.xsd">
<!--
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="*.do">strutsWrappingController</prop>
</props>
</property>
</bean>
<bean id="strutsWrappingController"
class="org.springframework.web.servlet.mvc.ServletWrappingController">
<property name="servletClass">
<value>org.apache.struts.action.ActionServlet</value>
</property>
<property name="servletName">
<value>action</value>
</property>
<property name="initParameters">
<props>
<prop key="config">/WEB-INF/struts-config.xml</prop>
</props>
</property>
</bean>
-->
</beans>
通过如上方式, 基本保证了各部分的松散耦合, web.xml也十分清晰.