以前在项目中使用Flex+spring+hibernate+mysql+blaze DS作为系统框架,今天抽时间把这个框架的搭建步骤做一个说明,以便下次开发使用。
开发工具使用的eclipse +flashbuider插件版本,具体的安装方法,请参照我另一篇文章,flex+java环境搭建.
开发环境和项目创建好之后,就开始整合spring和flex。
首先,在web.xml中加上flex session的监听器
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>xxx</display-name> <description>xxx</description> <context-param> <param-name>webAppRootKey</param-name> <param-value>webapp.xxx</param-value> </context-param> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath: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.util.Log4jConfigListener</listener-class> </listener> <servlet> <servlet-name>MessageBrokerServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- MessageBroker Servlet <servlet> <servlet-name>MessageBrokerServlet</servlet-name> <display-name>MessageBrokerServlet</display-name> <servlet-class>flex.messaging.MessageBrokerServlet</servlet-class> <init-param> <param-name>services.configuration.file</param-name> <param-value>/WEB-INF/flex/services-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> --> <servlet> <servlet-name>RDSDispatchServlet</servlet-name> <display-name>RDSDispatchServlet</display-name> <servlet-class>flex.rds.server.servlet.FrontEndServlet</servlet-class> <init-param> <param-name>useAppserverSecurity</param-name> <param-value>false</param-value> </init-param> <init-param> <param-name>messageBrokerId</param-name> <param-value>_messageBroker</param-value> </init-param> <load-on-startup>10</load-on-startup> </servlet> <servlet-mapping id="RDS_DISPATCH_MAPPING"> <servlet-name>RDSDispatchServlet</servlet-name> <url-pattern>/CFIDE/main/ide.cfm</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>MessageBrokerServlet</servlet-name> <url-pattern>/messagebroker/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>login.html</welcome-file> <welcome-file>login.htm</welcome-file> </welcome-file-list> <mime-mapping> <extension>doc</extension> <mime-type>application/msword</mime-type> </mime-mapping> <mime-mapping> <extension>xls</extension> <mime-type>application/msexcel</mime-type> </mime-mapping> <mime-mapping> <extension>pdf</extension> <mime-type>application/pdf</mime-type> </mime-mapping> <mime-mapping> <extension>zip</extension> <mime-type>application/zip</mime-type> </mime-mapping> <mime-mapping> <extension>rar</extension> <mime-type>application/rar</mime-type> </mime-mapping> <mime-mapping> <extension>txt</extension> <mime-type>application/txt</mime-type> </mime-mapping> <mime-mapping> <extension>chm</extension> <mime-type>application/mshelp</mime-type> </mime-mapping> <mime-mapping> <extension>mp3</extension> <mime-type>audio/x-mpeg</mime-type> </mime-mapping> <!-- for WebSphere deployment, please uncomment --> <!-- <resource-ref> <description>Flex Messaging WorkManager</description> <res-ref-name>wm/MessagingWorkManager</res-ref-name> <res-type>com.ibm.websphere.asynchbeans.WorkManager</res-type> <res-auth>Container</res-auth> <res-sharing-scope>Shareable</res-sharing-scope> </resource-ref> --> </web-app>
整个web.xml文件内容如上,可以直接使用,里面具体内容不做描述,如有不明白地方,可以直接百度。
下面是spring的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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:flex="http://www.springframework.org/schema/flex" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/flex http://www.springframework.org/schema/flex/spring-flex-1.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <context:annotation-config /> <context:component-scan base-package="cn.ccb.yn.acms" /> <flex:message-broker> <flex:exception-translator ref="flexExcetiionTranslator" /> <flex:message-service default-channels="my-amf" /> </flex:message-broker> <bean id="flexExcetiionTranslator" class="cn.wgy.common.util.FlexExcetiionTranslator"/> <bean id="AppDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${jdbc.driverClassName}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="minPoolSize" value="1" /> <property name="maxPoolSize" value="25" /> <property name="maxIdleTime" value="180" /> </bean> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 读取数据库配置文件结束 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> <property name="nestedTransactionAllowed" value="true"></property> </bean> <aop:config> <aop:advisor id="managerTx" pointcut="execution(* cn.wgy..*.ucc.*.*(..))" advice-ref="txAdvice" order="2" /> </aop:config> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" read-only="true" /> <tx:method name="find*" read-only="true" /> <tx:method name="*" rollback-for="BizException" /> </tx:attributes> </tx:advice> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource"> <ref bean="AppDataSource" /> </property> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <bean id="sessionFactory" name="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" abstract="false"> <property name="mappingDirectoryLocations"> <list> <value>classpath:/cn/wgy/common/model/</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.generate_statistics">true</prop> <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> <prop key="hibernate.cache.use_query_cache">true</prop> <prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <property name="eventListeners"> <map> <entry key="merge"> <bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener" abstract="false" /> </entry> </map> </property> <property name="dataSource"> <ref bean="AppDataSource" /> </property> </bean> </beans>
数据库连接配置文件:
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/acms?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull jdbc.username=root jdbc.password=ROOT # DBCP Pool settings jdbc.InitialSize=5 jdbc.MaxActive=10 jdbc.MaxIdle=5 jdbc.MaxWait=30000
java给flex提供接口类的写法
package cn.wgy.test.ucc.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.flex.remoting.RemotingDestination; import org.springframework.stereotype.Service; import cn.ccb.yn.acms.test.service.TestService; import cn.ccb.yn.acms.test.ucc.TestUcc; @Service @RemotingDestination public class TestUccImpl implements TestUcc{ @Autowired TestService testServiceImpl; public String getTest(){ return testServiceImpl.getTest(); } }
TestUccImpl就是flex页面用到的接口。
项目中会用到的jar包这列就不列出了,如果有需要,可以给我发邮件.itwgy@itwgy.com