Spring学习之旅-组件支撑篇(5)

84 阅读1分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第21天,点击查看活动详情


写在前面

作为一个java程序员,spring应该都不会陌生了吧?

对于大部分程序员来说,spring的入门,估计跑起一个框架,熟悉一下开发的流程,基本上就掌握spring框架的开发了。

随着开发年限的变大,我们不能仅仅是掌握到这个地步,更应该深入的学习spring框架。

这不,接下来就是spring框架的学习之旅了,希望可以帮助到大家。

一、事务支持

1.1 事务管理之XML方式

准备转账环境:

1.1.1 业务层

AccountService
AccountServiceImpl
public void transfer(String in, String out, double money) {
    dao.outMoney(out, money);
    
    //异常代码
    System.out.println(1/0);
    dao.inMoney(in, money);
}

1.1.2 持久层

AccountDao
AccountDaoImpl

1.1.3 spring配置


<!--配置数据源连接池(C3P0) -->
<bean id="dataSource" class= "com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method= "close ">
    <property nane= "driverClass" value= "com.mysql.jdbc.Driver" />
    <property name= "jdbcUrl" value= "jdbc:mysql://localhost:3306" />
    <property name= "user" value= "root" />
    <property name= "password" value= "root" />
</bean>

<bean class= "com.spring.dao.AccountDaoImpl ">
    <property name= "dataSource" ref= "datasource"></property>
</bean>

<context:component-scan base.package="com.spring.service"></context:component-scan>

1.1.4 单元测试代码

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext-tx.xml" })
public class TransactionTest {
    @Autowired
    private AccountService service;
    
    @Test
    public void test01() {
        service. transfer("小米""小明"100);
    }
}

1.1.5 配置事务管理的AOP

  • 平台事务管理器: DataSourceTransactionManager

<!--配置平台事务管理器-->
<bean id= "transactionManager" class= "org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <!--注入数据源-->
    <property name= "datasource" ref= "datasource"></property>
</bean>
  • 事务通知:
<tx:advice id="" transaction-manager="" />
<tx:advice id="txAdvice" transaction-manager= "transactionManager">
    <!--配置事务相关属性-->
    <tx:attributes>
        <!--对方法级别设置事务的隔离级别、事务传播行为-->
        <!--设置了默认的隔离级别和传播行为-->
        <tx:method name= "transfer*" />
        <!--提交订单-->
        <tx :method name= "submitOrder"/>
        <1--添加-->
        <tx:method name= "add*" />
        <tx:method name= "insert*" />
        <!--删除-->
        <tx:method name= "delete*" />
        <tx:method name= "remove*" />
        <!-- 修改-->
        <tx:method name= "update*" />
        <tx:method name= "modify*" />
        <!--查询-->
        <tx:method name= "find*" read-only= "true" />
        <tx:method name= "get*" read-only= "true" />
        <tx:method name= "query*" read-only= "true" />
    </tx:attributes>
</tx:advice>
  • AOP配置:

<aop:config>
    <aop:advisor advice-ref="" pointcut=""/>
</aop:config>
<!--配置AOP -->
<aop:config>
    <!-- 切入点:所有的业务层实现类中方法-->
    <aop:advisor advice-ref= "txAdvice" pointcut= "execution(* com.service.*.*(..))"/>
</aop:config>

1.2 事务管理之混合方式

  • service类上或者方法上加注解:
类上加@Transactional :表示该类中所有的方法都被事务管理

方法上加@Transactional :表示只有该方法被事务管理
  • 开启事务注解:
<!--配置平台事务管理器-->
<bean id="transactionManager" class="org.springframeworkjdbc.datasource.DataSourceTransactionManager">
    <!--注入数据源-->
    <property name="datasource" ref="dataSource"></property>
</bean>

<!-- 开启事务注解-->
<tx:annotation-driven transaction-manager="transactionManager"/>

1.3 事务管理之基于AspectJ的纯注解方式

@EnableTransactionManagement

好了,以上就是Spring学习之旅-组件支撑篇(5) 的全部内容了。

今天就先到这里了,后面的内容,留下次分享了,先溜了!!!^_^

如果觉得写得不错的,帮忙点赞、评论、收藏一下呗!!!

image.png