1.XML配置
<!-- 配置事务管理器 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置业务bean --> <bean id="personService" class="com.persia.service.impl.PersonServiceImpl"> <property name="ds" ref="dataSource"></property> </bean> <!-- 使用XML来使用事务管理--> <aop:config> <!-- 配置一个切面,和需要拦截的类和方法 --> <aop:pointcut id="transactionPointcut" expression="execution(* com.persia.service..*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut"/> </aop:config> <!-- 配置一个事务通知 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <!-- 方法以get开头的,不使用事务 --> <tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED"/> <!-- 其他方法以默认事务进行 --> <tx:method name="*"/> </tx:attributes> </tx:advice>
2.业务bean无需注解
public void delete(Integer id){ // TODO Auto-generated method stub jdbcTemplate.update("delete from person where id=?", new Object[]{id}, new int[]{java.sql.Types.INTEGER}); jdbcTemplate.update("delete from person1 where id=?", new Object[]{id}, new int[]{java.sql.Types.INTEGER}); //throw new RuntimeException("运行期例外"); }
3.Junit4测试
@Test
public void testDelete(){
ps.delete(5);
}
4.总结:
可见xml配置事务起了作用,是要也是利用了aop,在拦截到方法的时候为方法加上事务。
建议用注解来使用事务。
本文通过一个具体的示例介绍了如何使用Spring框架通过XML配置文件实现事务管理。包括配置事务管理器、业务Bean以及使用AOP配置事务通知等内容。
使用XML配置事务&spm=1001.2101.3001.5002&articleId=97966751&d=1&t=3&u=580f8861fafd40a8a963eba7d118c4fd)
851

被折叠的 条评论
为什么被折叠?



