Spring的声明式事务学习
Spring的声明式事务顾名思义就是采用申明的方式来处理事务。这里所说的声明,就是指在配置文件中申明。用在Spring配置文件中申明式地处理事务来代替代码式的。这样的好处是业务逻辑(Dao)就不会意识到事务管理的存在,而且维护起来极其方便。
使用声明式事务管理时,通常要把我们的Dao交给一个代理,由其进行管理。这个代理一般spring里的:org.springwork.transaction.interceptor.TransactionProxyFactoryBean
我的配置清单如下:
<bean id="hibernateTestProxy"
/>
</property>
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributeSource">
<ref bean="transactionAttributeSource" />
</property>
</bean>
<bean id="transactionAttributeSource"
/>
</constructor-arg>
</bean>
<bean id="transactionInterceptor"
/>
</property>
<property name="transactionAttributeSource">
<ref bean="transactionAttributeSource2" />
</property>
</bean>
<!-- 这个属性表示所有在文中定义了的bean中以“execute”开头的方法都是事务方法-->
<bean id="transactionAttributeSource2"
/>
</property>
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributeSource">
<ref bean="transactionAttributeSource" />
</property>
</bean>
<bean id="transactionAttributeSource"
/>
</constructor-arg>
</bean>
<bean id="transactionInterceptor"
/>
</property>
<property name="transactionAttributeSource">
<ref bean="transactionAttributeSource2" />
</property>
</bean>
<!-- 这个属性表示所有在文中定义了的bean中以“execute”开头的方法都是事务方法-->
<bean id="transactionAttributeSource2"
class="org.springwork.transaction.interceptor.NameMatchTransactionAttributeSource">
<property name="properties">
<props>
<prop key="execute*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!--以上的属性缺点显而易见:整个程序只有一种策略。以下则可任意指定方法的策略。如需增加事务,只须添加<entry>元素-->
<bean id="transactionAttributeSource3"
class="org.springwork.transaction.interceptor.MethodMapTransactionAttributeSource">
<property name="methodMap">
<map>
<entry key="cn.sunrain.test.service.hibernate.HibernatetestService.execute*">
<>PROPAGATION_REQUIRED</>
</entry>
</map>
</property>
</bean>