使用Spring AOP 自动代理配置声明式事务
来源:互联网 发布:浪迹教育 知乎 编辑:程序博客网 时间:2024/05/17 02:52
我们可以使用下面两种方式消除proxyFactoryBean的繁重配置
1.继承parent bean
2.使用aop 自动代理
spring中的事务都是基于AOP的,我们同样可以使用自动代理消除TransactionProxyFactoryBean的重负实例
首先,我们要做任何自动通知一样,需要声明一个bean,成为DefaultAdvisorAutoProxyCreator
<bean id="org.springfranework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
</bean>
</bean>
DefaultAdvisorAutoProxyCreator将在应用上下文中遍历advisor,自动用它们来代理匹配advisor的pointcut的所有bean,对于事务,应该使用advisor是TransactionAttributeSourceAdvisor
<bean id="transactionAdvisor" class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
<constructor-arg>
<ref bean="transactionInterceptor"/>
</constructor-arg>
</bean>
<constructor-arg>
<ref bean="transactionInterceptor"/>
</constructor-arg>
</bean>
TransactionAttributeSourceAdvisor是一个羽翼丰满的AOP Advisor,同样,他有一个pointcut和一个advisor构成,这个pointcut是静态方法pointcut,他根据事物属性来源确定一个方法是否和一些事务属性关联,关于拦截器advisor,通过一个构造参数transactionInterceptor
<bean id="TransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributeSource">
<ref bean="transactionAttributeSource"/>
</property>
</bean>
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributeSource">
<ref bean="transactionAttributeSource"/>
</property>
</bean>
为自动代理选择一个transactionAttributeSource
<bean id="transactionAttributeSource" class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
<property name="properties">
<props>
<prop key="get*">PROPAGATION_SUPPORTS</prop>
</props>
</property>
</bean>
<property name="properties">
<props>
<prop key="get*">PROPAGATION_SUPPORTS</prop>
</props>
</property>
</bean>
关于属性源,我们有一个更好的解决方法
<bean id="transactionAttributeSource" class="org.springframework.transaction.interceptor.MethodMapTransactionAttributeSource">
<property name="methodMap">
<map>
<entry key="com.springaction.service.CourseServiceImpl.get*">
<value>PROPAGATION_SUPPORT</value>
</entry>
</map>
</property>
</bean>
</bean>
等等还没有完,我们更好的选择是使用AttributesTransactionAttributeSouce,即元数据,我们使用方法事务化和非事务化,只不过是添加合适的元数据到方法里而已
- 使用Spring AOP 自动代理配置声明式事务
- 使用Spring AOP 自动代理配置声明式事务
- spring声明式事务【使用AOP】
- Spring基于tx/aop声明式事务配置
- spring声明式事务配置方法 aop:config切入
- Spring声明式事务配置——AOP方式
- Spring基于tx/aop声明式事务配置
- Spring AOP动态代理 /声明式事务管理与配置介绍
- Spring使用TransactionProxyFactoryBean声明式事务配置
- Spring使用TransactionProxyFactoryBean声明式事务配置
- 使用Spring的声明式事务----AOP方式
- 使用Spring的声明式事务----AOP方式
- Spring AOP使用配置介绍(三):自动创建代理
- Spring Aop实现声明式事务
- Spring aop实现声明式事务
- Spring AOP配置声明式事务,数据业务层捕获异常,事务控制处理
- spring 配置声明式事务
- spring声明式事务配置
- 求助 用VB作的类似按键精灵的小软件!有高手帮个忙
- 有CCS的情况下,如何动态设置gridview的字体颜色
- pragma指令简介
- 销量统计 柱状图
- COM技术初探(二)
- 使用Spring AOP 自动代理配置声明式事务
- ISAPI_Rewrite 实用的 RewriteRule (转静态)
- 大家好
- mapping覆盖的结果
- 关于JavaScript处理时间跨度差问题的一个小细节
- 别让Hibernate偷走了你的标识符
- Oracle 函数大全(字符串函数,数学函数,日期函数,逻辑运算函数,其他函数)
- 如何判断你所在企业的企业文化
- 一个简单计算器【解析表达式】