AOP在事务管理中的应用原理是什么?

2026-05-28 03:051阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计346个文字,预计阅读时间需要2分钟。

AOP在事务管理中的应用原理是什么?

/+作者:+老男孩老师 +CSDN认证讲师 +51CTO高级讲师 +腾讯课堂认证讲师 +网易云课堂认证讲师 +华为开发者学院认证讲师 +爱奇艺千人行会员 +在这里给广大技术爱好者分享技术、知识+/**

/*
*作者:呆萌老师
*☑csdn认证讲师
*☑51cto高级讲师
*☑腾讯课堂认证讲师
*☑网易云课堂认证讲师
*☑华为开发者学堂认证讲师
*☑爱奇艺千人名师计划成员
*在这里给大家分享技术、知识和生活
*各种干货,记得关注哦!
*/

基于aop的事务管理
1.修改配置文件 ,加入以下配置

AOP在事务管理中的应用原理是什么?

<!-- 事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!-- 为连接点指定事务属性 -->
<tx:method name="transfer" isolation="DEFAULT" propagation="REQUIRED" rollback-for="MyException"/>
</tx:attributes>
</tx:advice>

<aop:config>
<!-- 切入点配置 -->
<aop:pointcut expression="execution(* *..service.*.*(..))" id="point"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="point"/>
</aop:config>

2.service层的事务注解去掉

3.测试

@Test
public void fun()
{
ClassPathXmlApplicationContext classPathXmlApplicationContext=new ClassPathXmlApplicationContext("/ApplicationContext.xml");

ICardInfoService cardInfoService= classPathXmlApplicationContext.getBean("cardInfoService",ICardInfoService.class);

try {
cardInfoService.transfer(1,2,250.0);
} catch (MyException e) {
e.printStackTrace();
}

}

本文共计346个文字,预计阅读时间需要2分钟。

AOP在事务管理中的应用原理是什么?

/+作者:+老男孩老师 +CSDN认证讲师 +51CTO高级讲师 +腾讯课堂认证讲师 +网易云课堂认证讲师 +华为开发者学院认证讲师 +爱奇艺千人行会员 +在这里给广大技术爱好者分享技术、知识+/**

/*
*作者:呆萌老师
*☑csdn认证讲师
*☑51cto高级讲师
*☑腾讯课堂认证讲师
*☑网易云课堂认证讲师
*☑华为开发者学堂认证讲师
*☑爱奇艺千人名师计划成员
*在这里给大家分享技术、知识和生活
*各种干货,记得关注哦!
*/

基于aop的事务管理
1.修改配置文件 ,加入以下配置

AOP在事务管理中的应用原理是什么?

<!-- 事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!-- 为连接点指定事务属性 -->
<tx:method name="transfer" isolation="DEFAULT" propagation="REQUIRED" rollback-for="MyException"/>
</tx:attributes>
</tx:advice>

<aop:config>
<!-- 切入点配置 -->
<aop:pointcut expression="execution(* *..service.*.*(..))" id="point"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="point"/>
</aop:config>

2.service层的事务注解去掉

3.测试

@Test
public void fun()
{
ClassPathXmlApplicationContext classPathXmlApplicationContext=new ClassPathXmlApplicationContext("/ApplicationContext.xml");

ICardInfoService cardInfoService= classPathXmlApplicationContext.getBean("cardInfoService",ICardInfoService.class);

try {
cardInfoService.transfer(1,2,250.0);
} catch (MyException e) {
e.printStackTrace();
}

}