本系列教程目标:使初学者了解富网络应用概念,理解并掌握以下四种架构方法。 1. Flex + BlazeDS + Spring(< 2.5.6) + iBATIS + Cairngorm 2. Flex + BlazeDS + Spring BlazeDS Integration + Spring(>= 2.5.6) + iBATIS + Cairngorm 3. Flex + BlazeDS + Spring(< 2.5.6) + iBATIS + pureMVC 4. Flex + BlazeDS + Spring BlazeDS Integration + Spring(>= 2.5.6) + iBATIS + pureMVC |
注意事项:
以下内容基于“Flex4系列教程之八”中最后形成的 sampleApp 项目。
准备所需 jar 包
将以下 jar 包拷贝到 sampleApp 项目的 lib 下
- Spring Framework dependencies
org.aspectj 内的 com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
Spring Framework 的事务管理类型
综合性的事务支持是 Spring Framework 倍受欢迎的原因之一。Spring Framework 有两种事务管理方式:声明式事务管理和编程式事务管理。前者因为“对代码影响最小“和“非侵入性”而较为流行。
配置声明式事务
Spring Framework 的声明式事务通过 AOP 思想实现。
- 制定事务管理规则
常见的是对 Service 层进行事务管理,我们也不例外。我们约定对 Service 接口内定义的方法实行以下事务上下文语义:- 以 get 开头的方法:只读(read-only)
- 以 insert 开头的方法:读写(read-write)
- 以 update 开头的方法:读写(read-write)
- 以 delete 开头的方法:读写(read-write)
- 配置
向 web-application-config.xml 文件追加以下内容:- 配置 PlatformTransactionManager bean,用于驱动事务
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean>
- 配置 advice
增加命名空间<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:flex="http://www.springframework.org/schema/flex" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
追加 advice
<tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="get*" read-only="true"/> <tx:method name="insert*"/> <tx:method name="update*"/> <tx:method name="delete*"/> </tx:attributes> </tx:advice>
- 配置切入点
增加命名空间<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:flex="http://www.springframework.org/schema/flex" xmlns:context="http://www.springframework.org/schema/context" 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-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">
追加切入点配置
<aop:config> <aop:pointcut id="serviceOperation" expression="execution(* *..*Service.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/> </aop:config>
[注:]加粗部分为 AspectJ 切入点表达式,我会在本系列教程之后详细介绍。
- 配置 PlatformTransactionManager bean,用于驱动事务
- 上面的配置实际上做了什么?
它们被用于围绕 Service 对象创建相应的事务代理,此代理会用 advice 配置。这样当 Service 中的方法在代理上执行时相应的事务也就启动了。 - 运行 sampleApp
附件:
Tagged BlazeDS, Cairngorm, Flash Builder, Flex, Flex教程,iBATIS,mybatis, pureMVC,RIA,Spring Framework
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。